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 statement to achieve the R Else If Statement result. 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 in R programming 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 statements effectively by executing them sequentially. R Else If Statement will check for the first expression, and if the expression is TRUE, then it will execute the statements present 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, Expression 2 is TRUE, for example:

x = 98, y = 65

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

Expression 2: else if (x > y) //TRUE

In these situations, statements under the Expression 1 executed because R ELSE IF condition will only be executed if it’s previous IF or ELSE IF statement fails.

Flow Chart of an R Else If Statement

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

Flow Chart for R Else If Statement

R Else If Statement example

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

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

# R Else If Statement Example

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

if (my.marks >= 550) {
  print("You are eligible for Full Scholarship")
} else if (my.marks >= 490)  {
  print("You are eligible for 50% Scholarship")
} else if (my.marks >= 400)  {
  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 R Else screenshot below, you can observe that We entered my.marks = 562. Here first If condition is TRUE so, statements inside the first if block will be executed.

R Else If Statement 1

OUTPUT 2: Here, we enter my.marks = 510 means 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 statements within this block. Although else if (my.marks >= 400) condition is TRUE, but it won’t check this condition.

R Else If Statement 2

We are going to enter my.marks = 420 means first two statements 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 statements inside this block.

R Else If Statement 3

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

R Else If Statement 4