Chapter 16 Control structures and loops

  • Loops are helpful in various situation.. whenever repeating something several times, e.g. a function on several variables/data sets
  • If Argument
    • if (argument1) {argument2} else {argument3}
    • If argument1 is TRUE then execute argument2, if not execute argument3
  • For loops
    • for(i in Sequence){argument1}
    • Command in one line or several lines but enclosed by curly brackets
    • i is the placeholder across which the loop runs
    • Sequence is a vector of values
    • For the values in Sequence the argument1 is executed
    • print(): Print objects within loop consecutively


  • While loops
    • while(argument1){argument2}
    • argument2 is executed as long as argument1 is TRUE
  • Repeat loops
    • repeat{argument1; argument2; if(argument3) break}
    • argument1 and argument2 are repeated until argument3 is TRUE