Control Statements in R Programming

• Difficulty Level : Basic
• Last Updated : 01 Jun, 2020

Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements. These structures are used to make a decision after assessing the variable. In this article, we’ll discuss all the control statements with the examples.

In R programming, there are 8 types of control statements as follows:

if condition

This control structure checks the expression provided in parenthesis is true or not. If true, the execution of the statements in braces {} continues.

Syntax:

if(expression){
statements
....
....
}

Example:

 x <- 100  if(x > 10){print(paste(x, "is greater than 10"))}

Output:

 "100 is greater than 10"

if-else condition

It is similar to if condition but when the test expression in if condition fails, then statements in else condition are executed.

Syntax:

if(expression){
statements
....
....
}
else{
statements
....
....
}

Example:

 x <- 5  # Check value is less than or greater than 10if(x > 10){  print(paste(x, "is greater than 10"))}else{  print(paste(x, "is less than 10"))}

Output:

 "5 is less than 10"

for loop

It is a type of loop or sequence of statements executed repeatedly until exit condition is reached.

Syntax:

for(value in vector){
statements
....
....
}

Example:

 x <- letters[4:10]  for(i in x){  print(i)}

Output:

 "d"
 "e"
 "f"
 "g"
 "h"
 "i"
 "j"

Nested loops

Nested loops are similar to simple loops. Nested means loops inside loop. Moreover, nested loops are used to manipulate the matrix.

Example:

 # Defining matrixm <- matrix(2:15, 2)  for (r in seq(nrow(m))) {  for (c in seq(ncol(m))) {    print(m[r, c])  }}

Output:

 2
 4
 6
 8
 10
 12
 14
 3
 5
 7
 9
 11
 13
 15

while loop

while loop is another kind of loop iterated until a condition is satisfied. The testing expression is checked first before executing the body of loop.

Syntax:

while(expression){
statement
....
....
}

Example:

 x = 1  # Print 1 to 5while(x <= 5){  print(x)  x = x + 1}

Output:

 1
 2
 3
 4
 5

repeat loop and break statement

repeat is a loop which can be iterated many number of times but there is no exit condition to come out from the loop. So, break statement is used to exit from the loop. break statement can be used in any type of loop to exit from the loop.

Syntax:

repeat {
statements
....
....
if(expression) {
break
}
}

Example:

 x = 1  # Print 1 to 5repeat{  print(x)  x = x + 1  if(x > 5){    break   }}

Output:

 1
 2
 3
 4
 5

return statement

return statement is used to return the result of an executed function and returns control to the calling function.

Syntax:

return(expression)

Example:

 # Checks value is either positive, negative or zerofunc <- function(x){  if(x > 0){    return("Positive")  }else if(x < 0){    return("Negative")  }else{    return("Zero")  }}  func(1)func(0)func(-1)

Output:

 "Positive"
 "Zero"
 "Negative"

next statement

next statement is used to skip the current iteration without executing the further statements and continues the next iteration cycle without terminating the loop.

Example:

 # Defining vectorx <- 1:10  # Print even numbersfor(i in x){  if(i%%2 != 0){    next #Jumps to next loop  }  print(i)}

Output:

 2
 4
 6
 8
 10

My Personal Notes arrow_drop_up