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 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 this Programming with an example. Before we get into the Else if in this programming example, Let us see the syntax

R Else If Statement Syntax

The basic syntax of the Else If Statement in this 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 can handle multiple statements effectively by executing them sequentially. The 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 are executed because the 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 this Programming

Else If Statement Flow Chart

R Else If Statement Example

This else if program allows the user to enter their grand total (total of 6 subject marks). Using Else if code, 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 R Programming

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 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 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 statements within this block. Although else if (my.marks >= 400) condition is TRUE, it won’t check this condition.

"Congratulations!!"
"You are eligible for 50% Scholarship"

We are going to enter my.marks = 420 means the 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.

"Congratulations!!"
"You are eligible for 10% Scholarship"

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.

"You are NOT eligible for Scholarship"
"We are really Sorry for You"