Nested If Else in R

Place one If Statement inside another If Statement called as Nested If Else in R Programming. The If Else statement allows us to print different statements depending upon the expression result (TRUE, or FALSE). Sometimes we have to check further when the condition is TRUE. In these situations, we can use this Nested If Else concept in R but, please be careful while using it.

For example, every person is eligible to work if the person is 18 years old or above. But, the government or any company will not give a job to every person. So, we use another If Statement also called Nested If Else Statement in R programming to check his previous experience, educational background, or any job-specific requirements. 

In this article, we show you how to write nested If Else statements in R Programming language with example. Before we get into the example, let us see the syntax behind the R Nested If Else statement.

Nested If Else in R Syntax

The basic syntax of the Nested If Else Statement in R Programming language is as follows:

if (Boolean_Expression 1)  {
     #Boolean_Expression 1 result is TRUE then, it will check for Boolean_Expression 2
     if (Boolean_Expression 2)  {
          #Boolean_Expression 2 result is TRUE, then these statements will be executed
          Boolean_Expression 2 True statements
     } else {
          #Boolean_Expression 2 result is FALSE then, these statements will be executed
          Boolean_Expression 2 False statements
} else  {
     #If the Boolean_Expression 1 result is FALSE, these statements will be executed
     Boolean_Expression 1 False statements
}

Nested If Else in R Flow Chart

The following picture shows you the flow chart of the Nested If statement in R Programming.

  • If the Test Condition 1 is FALSE, then STATEMENT 3 executed.
  • If Test Condition 1 is TRUE, then it will check for the Test Condition 2. And if it is TRUE, then STATEMENT 1 executed. Otherwise, STATEMENT 2 executed.
FLOW CHART of a Nested If in R programming

Nested If Else in R Programming Example

This R Nested If Else program allows the user to enter their age, and then we are going to store it in the variable my.age. If the user-specified age is less than 18, we are going to print two statements. If the condition fails, we check one more condition (R Nested If Else), if it succeeds, we print something. And If the nested condition fails, we print some other thing.

# Nested IF Else in R Programming Example

my.age <- as.integer(readline(prompt="Please Enter your Age: "))

if (my.age < 18) {
  print("You are Not a Major.") 
  print("You are Not Eligible to Work")
} else {
  if (my.age >= 18 && my.age <= 60 )  {
    print("You are Eligible to Work")
    print("Please fill the Application Form and Email to us")
  } else {
    print("As per the Government Rules, You are too Old to Work")
    print("Please Collect your pension!")
  }  
}
print("This Message is from Outside the Nested IF Else Statement")

Within the R Nested If Else program example, If the specified person’s age is less than 18, then he is not eligible to work. If the age is greater than or equal to 18, the first condition fails, it checks the else statement. Within the Else statement, there is another Boolean expression (called as Nested If Else).

  • The R Nested If Else Statement checks whether the person’s age is greater than or equal to 18 and less than or equal to 60. If the expression is TRUE, then he can apply for the job.
  • If the expression result is FALSE, then he is too old to work as per the government.
  • We placed one print statement outside the If Else block, and it will execute irrespective of condition result.

Please refer to If Else statement and If Statement articles in R Programming.

OUTPUT 1: We are going to enter age = 35 means the first IF condition is FALSE. It will go to else block, and within the else block, it will check the if (age >= 18 && age <=60), which is TRUE. So, it will print the statements inside this block.

Nested If Else in R Programming 2

OUTPUT 2: From the below R Nested If Else screenshot, you can observe that we entered the age = 14 means, the first If condition is TRUE. That’s why the statements inside the first if block is executed.

Nested If Else in R Programming 1

This time, we are going to enter age = 67 means first If condition is FALSE. It will go to else block, and within the else block, it will check the expression if (age >= 18 && age <=60), which is FALSE. That’s why it will print the statements inside Nested else block.

Nested If Else in R Programming 3