Decision Making in R Programming - if, if-else, if-else-if ladder, nested if-else, and switch - GeeksforGeeks

08 Jul, 2021
Decision making is about deciding the order of execution of statements based on certain conditions. In decision making programmer needs to provide some condition which is evaluated by the program, along with it there also provided some statements which are executed if the condition is true and optionally other statements if the condition is evaluated to be false.
The decision making statement in R are as followed:
if statement
if-else statement
if-else-if ladder
nested if-else statement
switch statement
Keyword if tells compiler that this is a decision control instruction and the condition following the keyword if is always enclosed within a pair of parentheses. If the condition is TRUE the statement gets executed and if condition is FALSE then statement does not get executed.
Syntax:
          if(condition is true){
                execute this statement
           }
Flow Chart:                        
Example: 
r
# R program to illustrate# if statementa <- 76b <- 67 # TRUE conditionif(a > b){    c <- a - b    print("condition a > b is TRUE")    print(paste("Difference between a, b is : ", c))} # FALSE conditionif(a < b){    c <- a - b    print("condition a < b is TRUE")    print(paste("Difference between a, b is : ", c))}
Output: 
[1] "condition a > b is TRUE"
[1] "Difference between a, b is :  9"
If-else, provides us with an optional else block which gets executed if the condition for if block is false.  If the condition provided to if block is true then the statement within the if block gets executed, else the statement within the else block gets executed.
Syntax:
          if(condition is true) {
              execute this statement
          } else {
             execute this statement 
          }
Flow Chart:
Example : 
r
# R if-else statement Examplea <- 67b <- 76  # This example will execute else blockif(a > b){    c <- a - b    print("condition a > b is TRUE")    print(paste("Difference between a, b is : ", c))} else{    c <- a - b    print("condition a > b is FALSE")    print(paste("Difference between a, b is : ", c))}
Output: 
[1] "condition a > b is FALSE"
[1] "Difference between a, b is :  -9"
It is similar to if-else statement, here the only difference is that an if statement is attached to else. If the condition provided to if block is true then the statement within the if block gets executed, else-if the another condition provided is checked and if true then  the statement within the block gets executed.
Syntax:
         if(condition 1 is true) {
              execute this statement
         } else if(condition 2 is true) {
             execute this statement
         } else {
             execute this statement
        }
Flow Chart: 
Example : 
r
# R if-else-if ladder Examplea <- 67b <- 76c <- 99  if(a > b && b > c){    print("condition a > b > c is TRUE")} else if(a < b && b > c){    print("condition a < b > c is TRUE")} else if(a < b && b < c){    print("condition a < b < c  is TRUE")}
Output: 
[1] "condition a < b < c  is TRUE"
When we have an if-else block as an statement within an if block or optionally within an else block, then it is called as nested if else statement. When an if condition is true then following child if condition is validated and if the condition is wrong else statement is executed, this happens within parent if condition. If parent if condition is false then else block is executed with also may contain child if else statement.
Syntax:
 if(parent condition is true) { 
              if( child condition 1 is true) {
                 execute this statement
             } else { 
                execute this statement
            }
     } else {
            if(child condition 2 is true) {
                execute this statement
            } else {
               execute this statement
           }
     }
Flow Chart: 
Example: 
r
# R Nested if else statement Examplea <- 10b <- 11  if(a == 10){    if(b == 10)    {        print("a:10 b:10")       } else    {        print("a:10 b:11")        }} else{    if(a == 11)    {        print("a:11 b:10")    } else    {        print("a:11 b:11")    }}
Output: 
[1] "a:10 b:11"
In this switch function expression is matched to list of cases. If a match is found then it prints that case’s value. No default case is available here. If no case is matched it outputs NULL as shown in example.
Syntax:
switch (expression, case1, case2, case3,...,case n )
Flow Chart :
Example: 
r
# R switch statement example # Expression in terms of the index valuex <- switch(    2,           # Expression    "Geeks1",    # case 1    "for",       # case 2    "Geeks2"     # case 3)print(x) # Expression in terms of the string valuey <- switch(    "GfG3",              # Expression    "GfG0"="Geeks1",     # case 1    "GfG1"="for",        # case 2    "GfG3"="Geeks2"      # case 3)print(y) z <- switch(    "GfG",               # Expression    "GfG0"="Geeks1",     # case 1    "GfG1"="for",        # case 2    "GfG3"="Geeks2"      # case 3)print(z)   print(z)
Output: 
[1] "for"
[1] "Geeks2"
NULL
gabaa406
Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change column name of a given DataFrame in R
How to Replace specific values in column in R DataFrame ?
Adding elements in a vector in R programming - append() method
Loops in R (for, while, repeat)
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming
How to Change Axis Scales in R Plots?
Remove rows with NA in one column of R DataFrame