| 42333 |
ripley |
1 |
% File src/library/base/man/Control.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 76628 |
ripley |
3 |
% Copyright 1995-2019 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 2 |
r |
6 |
\name{Control}
|
| 56186 |
murdoch |
7 |
\alias{Control}
|
| 18286 |
ripley |
8 |
\alias{if}
|
|
|
9 |
\alias{else}
|
|
|
10 |
\alias{for}
|
| 42950 |
ripley |
11 |
\alias{in}
|
| 18286 |
ripley |
12 |
\alias{while}
|
|
|
13 |
\alias{repeat}
|
|
|
14 |
\alias{break}
|
|
|
15 |
\alias{next}
|
| 2 |
r |
16 |
\title{Control Flow}
|
| 30824 |
ripley |
17 |
\description{
|
|
|
18 |
These are the basic control-flow constructs of the \R language. They
|
|
|
19 |
function in much the same way as control statements in any Algol-like
|
| 42950 |
ripley |
20 |
language. They are all \link{reserved} words.
|
| 30824 |
ripley |
21 |
}
|
| 2 |
r |
22 |
\usage{
|
|
|
23 |
if(cond) expr
|
|
|
24 |
if(cond) cons.expr else alt.expr
|
| 30824 |
ripley |
25 |
|
| 2 |
r |
26 |
for(var in seq) expr
|
|
|
27 |
while(cond) expr
|
|
|
28 |
repeat expr
|
|
|
29 |
break
|
|
|
30 |
next
|
|
|
31 |
}
|
| 30824 |
ripley |
32 |
\arguments{
|
|
|
33 |
\item{cond}{A length-one logical vector that is not \code{NA}.
|
| 72358 |
maechler |
34 |
Conditions of length greater than one are currently accepted with a
|
|
|
35 |
warning, but only the first element is used. An error is signalled
|
|
|
36 |
instead when the environment variable \env{_R_CHECK_LENGTH_1_CONDITION_}
|
|
|
37 |
is set to true. Other types are coerced to logical
|
| 43281 |
ripley |
38 |
if possible, ignoring any class.
|
| 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
|
| 76628 |
ripley |
43 |
factor value will be coerced to a character vector. As from \R
|
| 77234 |
ripley |
44 |
4.0.0 this can be a long vector.}
|
| 30824 |
ripley |
45 |
\item{expr, cons.expr, alt.expr}{
|
|
|
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,
|
| 48054 |
pd |
72 |
\code{var} remains as a variable containing its latest value.
|
|
|
73 |
|
| 19281 |
maechler |
74 |
}
|
| 36578 |
ripley |
75 |
\value{
|
|
|
76 |
\code{if} returns the value of the expression evaluated, or
|
| 51215 |
ripley |
77 |
\code{NULL} invisibly if none was (which may happen if there is no
|
|
|
78 |
\code{else}).
|
| 36578 |
ripley |
79 |
|
| 48748 |
luke |
80 |
\code{for}, \code{while} and \code{repeat} return \code{NULL} invisibly.
|
| 37590 |
ripley |
81 |
\code{for} sets \code{var} to the last used element of \code{seq},
|
|
|
82 |
or to \code{NULL} if it was of length zero.
|
| 36578 |
ripley |
83 |
|
| 48748 |
luke |
84 |
\code{break} and \code{next} do not return a value as they transfer
|
|
|
85 |
control within the loop.
|
| 36578 |
ripley |
86 |
}
|
|
|
87 |
|
| 24300 |
ripley |
88 |
\references{
|
|
|
89 |
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
|
|
|
90 |
\emph{The New S Language}.
|
| 47262 |
ripley |
91 |
Wadsworth & Brooks/Cole.
|
| 24300 |
ripley |
92 |
}
|
| 2 |
r |
93 |
\seealso{
|
| 20289 |
ripley |
94 |
\code{\link{Syntax}} for the basic \R syntax and operators,
|
| 51218 |
ripley |
95 |
\code{\link{Paren}} for parentheses and braces.
|
| 61433 |
ripley |
96 |
|
| 51218 |
ripley |
97 |
\code{\link{ifelse}}, \code{\link{switch}} for other ways to control flow.
|
| 2 |
r |
98 |
}
|
|
|
99 |
\examples{
|
|
|
100 |
for(i in 1:5) print(1:i)
|
| 19281 |
maechler |
101 |
for(n in c(2,5,10,20,50)) {
|
| 41508 |
ripley |
102 |
x <- stats::rnorm(n)
|
| 61434 |
ripley |
103 |
cat(n, ": ", sum(x^2), "\n", sep = "")
|
| 19281 |
maechler |
104 |
}
|
| 61168 |
ripley |
105 |
f <- factor(sample(letters[1:5], 10, replace = TRUE))
|
|
|
106 |
for(i in unique(f)) print(i)
|
| 19281 |
maechler |
107 |
}
|
| 286 |
maechler |
108 |
\keyword{programming}
|
|
|
109 |
\keyword{iteration}
|
|
|
110 |
\keyword{logic}
|