Control                 package:base                 R Documentation

_C_o_n_t_r_o_l _F_l_o_w

_D_e_s_c_r_i_p_t_i_o_n:

     These are the basic control-flow constructs of the R language. 
     They function in much the same way as control statements in any
     algol-like language.

_U_s_a_g_e:

     if(cond) expr
     if(cond) cons.expr  else  alt.expr
     for(var in seq) expr
     while(cond) expr
     repeat expr
     break
     next

_D_e_t_a_i_l_s:

     Note that 'expr' and 'cons.expr', etc, in the Usage section above
     means an _expression_ in a formal sense.  This is either a simple
     expression or a so called _compound expression_, usually of the
     form '{ expr1 ; expr2 }'.

     Note that it is a common mistake to forget putting braces ('{ ..
     }') around your statements, e.g., after 'if(..)' or 'for(....)'.
     In particular, you should not have a newline between '}' and 
     'else' to avoid a syntax error in entering a 'if ... else'
     construct at the keyboard or via 'source'. For that reason, one
     (somewhat extreme) attitude of defensive programming uses braces
     always, e.g., for 'if' clauses.

     The index 'seq' in a 'for' loop is evaluated at the start of the
     loop; changing it subsequently does not affect the loop.

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'Syntax' for the basic R syntax and operators, 'Paren' for
     parentheses and braces; further, 'ifelse', 'switch'.

_E_x_a_m_p_l_e_s:

     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")
     }

