The R Project SVN R

Rev

Rev 61433 | Rev 70295 | Go to most recent revision | 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
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 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}.
34
    Conditions of length greater than one are accepted with a warning, but
43281 ripley 35
    only the first element is used.  Other types are coerced to logical
36
    if possible, ignoring any class.
30824 ripley 37
  }
38
  \item{var}{A syntactical name for a variable.}
37590 ripley 39
  \item{seq}{An expression evaluating to a vector (including a list and
47034 ripley 40
    an \link{expression}) or to a \link{pairlist} or \code{NULL}.  A
41
    factor value will be coerced to a character vector.}
30824 ripley 42
  \item{expr, cons.expr, alt.expr}{
43
    An \emph{expression} in a formal sense.  This is either a
44
    simple expression or a so called \emph{compound expression}, usually
45
    of the form \code{\{ expr1 ; expr2 \}}.
46
  }
2 r 47
}
19281 maechler 48
\details{
30824 ripley 49
  \code{break} breaks out of a \code{for}, \code{while} or \code{repeat}
39252 ripley 50
  loop; control is transferred to the first statement outside the
51
  inner-most loop. \code{next} halts the processing of the current
52
  iteration and advances the looping index.  Both \code{break} and
53
  \code{next} apply only to the innermost of nested loops.
61433 ripley 54
 
30824 ripley 55
  Note that it is a common mistake to forget to put braces (\code{\{ .. \}})
25099 hornik 56
  around your statements, e.g., after \code{if(..)} or \code{for(....)}.
61433 ripley 57
  In particular, you should not have a newline between \code{\}} and
27041 ripley 58
  \code{else} to avoid a syntax error in entering a \code{if ... else}
59
  construct at the keyboard or via \code{source}.
19281 maechler 60
  For that reason, one (somewhat extreme) attitude of defensive programming
33449 rgentlem 61
  is to always use braces, e.g., for \code{if} clauses.
21033 tlumley 62
 
48054 pd 63
  The \code{seq} in a \code{for} loop is evaluated at the start of
55555 ripley 64
  the loop; changing it subsequently does not affect the loop.  If
48752 ripley 65
  \code{seq} has length zero the body of the loop is skipped. Otherwise the
48054 pd 66
  variable \code{var} is assigned in turn the value of each element of
67
  \code{seq}. You can assign to \code{var} within the body of the loop,
55555 ripley 68
  but this will not affect the next iteration.  When the loop terminates,
48054 pd 69
  \code{var} remains as a variable containing its latest value.
70
 
19281 maechler 71
}
36578 ripley 72
\value{
73
  \code{if} returns the value of the expression evaluated, or
51215 ripley 74
  \code{NULL} invisibly if none was (which may happen if there is no
75
  \code{else}).
36578 ripley 76
 
48748 luke 77
  \code{for}, \code{while} and \code{repeat} return \code{NULL} invisibly.
37590 ripley 78
  \code{for} sets \code{var} to the last used element of \code{seq},
79
  or to \code{NULL} if it was of length zero.
36578 ripley 80
 
48748 luke 81
  \code{break} and \code{next} do not return a value as they transfer
82
  control within the loop.
36578 ripley 83
}
84
 
24300 ripley 85
\references{
86
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
87
  \emph{The New S Language}.
47262 ripley 88
  Wadsworth & Brooks/Cole.
24300 ripley 89
}
2 r 90
\seealso{
20289 ripley 91
  \code{\link{Syntax}} for the basic \R syntax and operators,
51218 ripley 92
  \code{\link{Paren}} for parentheses and braces.
61433 ripley 93
 
51218 ripley 94
  \code{\link{ifelse}}, \code{\link{switch}} for other ways to control flow.
2 r 95
}
96
\examples{
97
for(i in 1:5) print(1:i)
19281 maechler 98
for(n in c(2,5,10,20,50)) {
41508 ripley 99
   x <- stats::rnorm(n)
61434 ripley 100
   cat(n, ": ", sum(x^2), "\n", sep = "")
19281 maechler 101
}
61168 ripley 102
f <- factor(sample(letters[1:5], 10, replace = TRUE))
103
for(i in unique(f)) print(i)
19281 maechler 104
}
286 maechler 105
\keyword{programming}
106
\keyword{iteration}
107
\keyword{logic}