Control Flow Statements

The statements in R are executed sequentially from the top of the program to the bottom. Control flow statements break up the flow of the program execution using decision making, looping, and branching constructs. R has the standard control structures one would expect to see in a modern programming language.

Topics covered in this section are

  ➤ Decision making constructs: if-else, ifelse, and switch

  ➤ Looping constructs: for, while, and repeat

  ➤ Branching constructs: next, and break

Control Flow Statements

↪ if-else

The basic syntax for creating an if-else statement in R is

      if(boolean_expression_1) {
        Executes when the boolean_expression_1 is TRUE.
      } else if( boolean_expression 2) {
        Executes when the boolean_expression_1 is TRUE.
      } else if( boolean_expression 3) {
        Executes when the boolean_expression_1 is TRUE.
      } else {
        Executes when none of the above boolean_expression is TRUE.
      }

Example:

      x <- 10
      y <- 20
      if ( x > 5) {
        y <- 25
        y
      } else {
        y
      }

      ---Output---       [1] 25

Checking 'cherry' in fruits vector:

      x <- c("apple", "banana", "cherry", "dragon fruit", "elderberry")
      if ("cherry" %in% x) {
        print("cherry found - do something")
      } else {
        print("cherry not found - do something")
      }

      ---Output---       [1] "cherry found - do something"

Control Flow Statements

↪ ifelse

The ifelse construct is a compact and vectorized version of the if-else construct. Many functions in R take a vector as input and return a resultant vector. Vectorized functions are faster than applying the same functions to each element of the vector individually.

The syntax is

      ifelse(boolean_expression, x, y)
          # When boolean_expression is TRUE, x is returned or else y is returned.

Example:

      x <- 70
      ifelse(x > 50, "Pass", "Fail")

      ---Output---       [1] "Pass"

The boolean_expression could be a logical vector and then the return value is a vector with the same length as boolean_expression. The returned vector has element from 'x' if the corresponding value of boolean_expression is TRUE or from 'y' if the corresponding value of boolean_expression is FALSE.

      x <- c(10, 60, 49, 70)
      ifelse(x > 50, "Pass", "Fail")

      ---Output---       1] "Fail" "Pass" "Fail" "Pass"

Control Flow Statements

↪ switch

Unlike the if-then construct, the switch construct in R tests an expression against list of values.

The basic syntax for creating a switch statement is

      switch(expression, list)
           # If the expression evaluates to an integer, the corresponding element 
           # at the index pointed by the integer is returned.

Example

      x <- switch(2, "apple", "banana", "cherry",
                  "dragon fruit", "elderberry")
      x                                        # Outputs 2nd element from the list.

      ---Output---       [1] "banana"

If the integer value is out of range i.e greater than the number of items in the list or smaller than 1, NULL is returned

      x <- switch(6, "apple", "banana", "cherry",
                  "dragon fruit", "elderberry")   # Searching for 6th element
      x

      ---Output---       NULL
      x <- switch(0, "apple", "banana", "cherry",  # Searching for 0th element
                  "dragon fruit", "elderberry")    # Count starts from 1
      x

      ---Output---       NULL

If the expression evaluates to a character string, then that string is matched to the names of the elements.

      x <- switch("cherry", 
             banana = "Banana",
             mango  = "Mango",
             cherry = "Cherry",
             pear   = "Pear")
      x

      ---Output---       [1] "Cherry"

The first matching element is returned when there are multiple matches.

Control Flow Statements

↪ for loop

The for loop executes a statement iterating over the elements of an object such as list and vector sequence.

      for (var in seq) {
        statement
      } 

Example:

      x <- c("apple", "banana", "cherry", "dragon fruit", "elderberry")
      for (i in 1:5) {
        print(x[i])
      }

      ---Output---       [1] "apple"       [1] "banana"       [1] "cherry"       [1] "dragon fruit"       [1] "elderberry"

The sequence length should be the length of the vector 'x'. If the sequence is longer than the length of the vector 'x', the loop is iterating over the elements NA. If the sequence is shorter, the loop exits before iterating over all elements of the vector.

The seq_along() function generates an integer sequence of the length of the argument.

      for (i in seq_along(x)) {
        print(x[i])
      }

It is not necessary to use the sequence. The loop can be constructed like below.

      for (i in x) {
        print(i)
      }

Control Flow Statements

↪ while loop

While boolean_expression is TRUE, a while loop executes a statement repetitively until the boolean_expression is FALSE.

The syntax is

      while (boolean_expression) {
        statement
      }

Example:

      x <- c("apple", "banana", "cherry", "dragon fruit", "elderberry")
      i <- 1
      while ( i < 4 ) {
        print(x[i])
        i <- i + 1
      }

      ---Output---       [1] "apple"       [1] "banana"       [1] "cherry"

Control Flow Statements

↪ repeat loop

The repeat loop initiates an infinite loop. The break statement should be used for exiting the loop to avoid an infinite loop.

      x <- c("apple", "banana", "cherry", "dragon fruit", "elderberry")
      i <- 1
      repeat {
        if ( i > 5) break
        print(x[i])
        i <- i + 1
      }

      ---Output---       [1] "apple"       [1] "banana"       [1] "cherry"       [1] "dragon fruit"       [1] "elderberry"

Control Flow Statements

↪ next

The next branching construct is used to skip an iteration of a loop.

      x <- c("apple", "banana", "cherry", "dragon fruit", "elderberry")
      for (i in x) {
        if ( i == "cherry") next   # cherry will not be printed.
        print(i)
      }

Control Flow Statements

↪ break

The break branching construct is used to exit a loop immediately

      x <- c("apple", "banana", "cherry", "dragon fruit", "elderberry")
      for (i in x) {
        if ( i == "cherry") break   # break at cherry 
        print(i)
      }

Control Flow Statements

↪ Tip!

Looping in R is inefficient and time consuming when processing the rows or columns of a large dataset. It is recommended to use R’s built-in numerical and character functions in conjunction with the apply family of functions.

Control Flow Statements

↪ Summary

  1. Control structures allow programmers to control the flow of an R program.
  2. Basic decision making constructs in R are if-else, ifelse, and switch.
  3. Basic looping constructs in R are for, while, and repeat.
  4. Basic branching constructs are next, and break.