R Else If Statement

The R Else If Statement is an extension to If-Else, and it is very useful when we have to check several conditions. We can also use the Nested If Else to achieve the Else If Statement in R programming results. But, as the number of conditions increases, code complexity will also increase.

In this article, we show how to write Else If statements in R Programming with an example. Before we get into the Else if example, Let us see the syntax.

R Else If Statement Syntax

The basic syntax of the Else If Statement in R Programming language is

if (Boolean_Expression 1)  {
     Statement 1
} else if (Boolean_Expression 2)  {
     Statement 2
.........
} else if (Boolean_Expression N)  {
     Statement N
} else  {
     Default statements
}

The Else If statement in R can handle multiple expressions effectively by executing them sequentially. The Else If Statement will check for the first expression, and if the expression is TRUE, it will execute the code in that block. If the expression is FALSE, then it will check the Next one (Else If Boolean expression), and so on. There will be situations where Expression 1, and Expression 2 is TRUE, for example:

x = 98, y = 65

Expression 1: if (x != y) //TRUE

Expression 2: (x > y) //TRUE

In these situations, the code block under Expression 1 is executed because the ELSE IF condition will only be executed if its previous IF expressions fail.

Flow Chart of Else If

The following screenshot will show you the flow chart behind this Else If statement in R Programming.

R Else If Statement Flow Chart

R Else If Statement Example

This else if program allows the user to enter their grand total (total 6 subject marks). Using the Else if statement in R programming, we are going to calculate whether the user is eligible for a scholarship or not.

Please refer to If-Else and Nested If Else articles in Programming.

# Example

my.marks <- as.integer(readline(prompt="Please Enter your Total Marks: "))

if (my.marks >= 550) {
  print("Congratulations!!") 
  print("You are eligible for Full Scholarship")
} else if (my.marks >= 490)  {
  print("Congratulations!!") 
  print("You are eligible for 50% Scholarship")
} else if (my.marks >= 400)  {
  print("Congratulations!!") 
  print("You are eligible for 10% Scholarship")
} else  {
  print("You are NOT eligible for Scholarship")
  print("We are really Sorry for You")
}

OUTPUT 1: From the Else screenshot below, you can observe that We entered my.marks = 562. Here first, If the condition is TRUE, print a message inside the first if block will be executed.

R Else If Statement 1

OUTPUT 2: Here, we enter my.marks = 510 means the first If statement in R Else If program is FALSE. It will check the else if (my.marks >= 490), which is TRUE, so it will print the message within this block. Although else if (my.marks >= 400) condition is TRUE, it won’t check this condition.

Output 2

We are going to enter my.marks = 420 means the first two: IF condition, else if (my.marks >= 490) are FALSE. So, It will check the else if (my.marks >= 400), which is TRUE, so it will print code inside this block.

In the second time, we enter marks = 375 means all the If & Else If conditions Fail. So, It will print the statements within the else block.

R Else If Statement 3