The R Project SVN R

Rev

Rev 88581 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/Control.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88869 kalibera 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{Control}
85410 maechler 7
\title{Control Flow}
56186 murdoch 8
\alias{Control}
18286 ripley 9
\alias{if}
10
\alias{else}
11
\alias{for}
42950 ripley 12
\alias{in}
18286 ripley 13
\alias{while}
14
\alias{repeat}
15
\alias{break}
16
\alias{next}
85410 maechler 17
\alias{\%||\%}
30824 ripley 18
\description{
19
  These are the basic control-flow constructs of the \R language.  They
20
  function in much the same way as control statements in any Algol-like
42950 ripley 21
  language.  They are all \link{reserved} words.
30824 ripley 22
}
2 r 23
\usage{
24
if(cond) expr
25
if(cond) cons.expr  else  alt.expr
30824 ripley 26
 
2 r 27
for(var in seq) expr
28
while(cond) expr
29
repeat expr
30
break
31
next
85410 maechler 32
 
33
x \%||\% y
2 r 34
}
30824 ripley 35
\arguments{
36
  \item{cond}{A length-one logical vector that is not \code{NA}.
81848 ripley 37
    Other types are coerced to logical if possible, ignoring any class.
85410 maechler 38
    (Conditions of length greater than one are an error.)
30824 ripley 39
  }
40
  \item{var}{A syntactical name for a variable.}
37590 ripley 41
  \item{seq}{An expression evaluating to a vector (including a list and
70310 lawrence 42
    an \link{expression}) or to a \link{pairlist} or \code{NULL}.  A
83419 ripley 43
    factor value will be coerced to a character vector.  This can be a
44
    long vector.}
85410 maechler 45
  \item{expr, cons.expr, alt.expr, x, y}{
30824 ripley 46
    An \emph{expression} in a formal sense.  This is either a
76628 ripley 47
    simple expression or a so-called \emph{compound expression}, usually
30824 ripley 48
    of the form \code{\{ expr1 ; expr2 \}}.
49
  }
2 r 50
}
19281 maechler 51
\details{
30824 ripley 52
  \code{break} breaks out of a \code{for}, \code{while} or \code{repeat}
39252 ripley 53
  loop; control is transferred to the first statement outside the
54
  inner-most loop. \code{next} halts the processing of the current
55
  iteration and advances the looping index.  Both \code{break} and
56
  \code{next} apply only to the innermost of nested loops.
61433 ripley 57
 
30824 ripley 58
  Note that it is a common mistake to forget to put braces (\code{\{ .. \}})
25099 hornik 59
  around your statements, e.g., after \code{if(..)} or \code{for(....)}.
61433 ripley 60
  In particular, you should not have a newline between \code{\}} and
27041 ripley 61
  \code{else} to avoid a syntax error in entering a \code{if ... else}
62
  construct at the keyboard or via \code{source}.
19281 maechler 63
  For that reason, one (somewhat extreme) attitude of defensive programming
33449 rgentlem 64
  is to always use braces, e.g., for \code{if} clauses.
21033 tlumley 65
 
48054 pd 66
  The \code{seq} in a \code{for} loop is evaluated at the start of
55555 ripley 67
  the loop; changing it subsequently does not affect the loop.  If
48752 ripley 68
  \code{seq} has length zero the body of the loop is skipped. Otherwise the
48054 pd 69
  variable \code{var} is assigned in turn the value of each element of
70
  \code{seq}. You can assign to \code{var} within the body of the loop,
55555 ripley 71
  but this will not affect the next iteration.  When the loop terminates,
88869 kalibera 72
  \code{var} remains as a variable containing its latest value. The
73
  \code{seq} is implicitly unclassed. For classed objects, one can use
74
  \code{\link{as.list}} to prepare a list to iterate over or
75
  \code{\link{seq_along}} to get an index vector and then  explicitly extract
76
  individual elements (see also \code{\link{lapply}}).
48054 pd 77
 
85910 smeyer 78
  The null coalescing operator \code{\%||\%} is a simple 1-line function:
79
  \code{x \%||\% y} is an idiomatic way to call
85410 maechler 80
  \preformatted{
81
    if (is.null(x)) y else x
82
                             # or equivalently, of course,
83
    if(!is.null(x)) x else y }
85953 hornik 84
  Inspired by Ruby, it was first proposed by \I{Hadley Wickham}.
19281 maechler 85
}
36578 ripley 86
\value{
87
  \code{if} returns the value of the expression evaluated, or
51215 ripley 88
  \code{NULL} invisibly if none was (which may happen if there is no
89
  \code{else}).
36578 ripley 90
 
48748 luke 91
  \code{for}, \code{while} and \code{repeat} return \code{NULL} invisibly.
37590 ripley 92
  \code{for} sets \code{var} to the last used element of \code{seq},
93
  or to \code{NULL} if it was of length zero.
36578 ripley 94
 
48748 luke 95
  \code{break} and \code{next} do not return a value as they transfer
96
  control within the loop.
36578 ripley 97
}
98
 
24300 ripley 99
\references{
88581 hornik 100
  \bibshow{R:Becker+Chambers+Wilks:1988}
24300 ripley 101
}
2 r 102
\seealso{
20289 ripley 103
  \code{\link{Syntax}} for the basic \R syntax and operators,
51218 ripley 104
  \code{\link{Paren}} for parentheses and braces.
61433 ripley 105
 
51218 ripley 106
  \code{\link{ifelse}}, \code{\link{switch}} for other ways to control flow.
2 r 107
}
108
\examples{
109
for(i in 1:5) print(1:i)
19281 maechler 110
for(n in c(2,5,10,20,50)) {
41508 ripley 111
   x <- stats::rnorm(n)
61434 ripley 112
   cat(n, ": ", sum(x^2), "\n", sep = "")
19281 maechler 113
}
61168 ripley 114
f <- factor(sample(letters[1:5], 10, replace = TRUE))
115
for(i in unique(f)) print(i)
85410 maechler 116
 
117
res <- {}
118
res \%||\% "alternative result"
119
x <- head(x) \%||\% stop("parsed, but *not* evaluated..")
120
 
121
res <- if(sum(x) > 7.5) mean(x) # may be NULL
122
res \%||\% "sum(x) <= 7.5"
19281 maechler 123
}
286 maechler 124
\keyword{programming}
125
\keyword{iteration}
126
\keyword{logic}