Rev 24300 | Rev 27541 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{Control}\alias{Control}\alias{if}\alias{else}\alias{for}\alias{while}\alias{repeat}\alias{break}\alias{next}\title{Control Flow}\usage{if(cond) exprif(cond) cons.expr else alt.exprfor(var in seq) exprwhile(cond) exprrepeat exprbreaknext}\description{These are the basic control-flow constructs of the \R language. Theyfunction in much the same way as control statements in any algol-likelanguage.}\details{Note that \code{expr} and \code{cons.expr}, etc, in the Usage sectionabove means an \emph{expression} in a formal sense. This is either asimple expression or a so called \emph{compound expression}, usuallyof the form \code{\{ expr1 ; expr2 \}}.Note that it is a common mistake to forget putting braces (\code{\{ .. \}})around your statements, e.g., after \code{if(..)} or \code{for(....)}.For that reason, one (somewhat extreme) attitude of defensive programminguses braces always, e.g., for \code{if} clauses.The index \code{seq} in a \code{for} loop is evaluated at the start ofthe loop; changing it subsequently does not affect the loop.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\seealso{\code{\link{Syntax}} for the basic \R syntax and operators,\code{\link{Paren}} for parentheses and braces; further,\code{\link{ifelse}}, \code{\link{switch}}.}\examples{for(i in 1:5) print(1:i)for(n in c(2,5,10,20,50)) {x <- rnorm(n)cat(n,":", sum(x^2),"\n")}}\keyword{programming}\keyword{iteration}\keyword{logic}