Else If Statement in C

The Else If Statement in C is instrumental, while we have to test several conditions. Apart from the Else If, we can utilize the Nested If statement to accomplish the same. However, as the number of conditions rises, the code complexity will grow further.

Else If statement in C Programming language effectively handles multiple statements by sequentially executing them. The compiler will check for the first condition. If the condition result is TRUE, then the statements present in that block will run. If the result is FALSE, the compiler verifies the Next one (Else If condition) and so on.

Else If Statement in C Syntax

The syntax of the Else If statement is as follows:

if (condition 1)
          statements 1
else if (condition 2)
          statements 2
else if (condition 3)
          statements 3
else if (condition n)
          statements n
    default statements

There are some circumstances where both the condition1 and condition2 are TRUE, for instance:

x= 20, y=10

Condition 1: x > y //TRUE

Condition 2: x != y //TRUE

In these circumstances, code under Condition1 will execute. Because ELSE IF conditions will only execute if its prior IF or ELSE IF statement fails.

Else If Flow chart

The programming flow Chart behind this Else If Statement in C Programming

Flow Chart

Else If Statement in C Example

In this program, the user is asked to enter their total six subject marks. Using this Else if statement, we will decide whether the person is qualified for a scholarship or not

/* Example */

#include <stdio.h> 

int main() 
 int Totalmarks; 
 //Imagine you have 6 subjects and Grand total is 600 
 printf("Please Enter your Total Marks\n" ); 
 scanf( "%d", &Totalmarks ); 

 if (Totalmarks >= 540) 
    printf("You are eligible for Full Scholarship\n");
 else if (Totalmarks >= 480) 
    printf("You are eligible for 50 Percent Scholarship\n");
 else if (Totalmarks >= 400) 
    printf("You are eligible for 10 Percent Scholarship\n");
    printf("You are Not eligible for Scholarship\n");
    printf("We are really Sorry for You\n");

OUTPUT 1: For this example, we will provide Totalmarks = 570. Here first, If condition is TRUE. Please refer to the Nested If statement article as well.

Else If Statement in C Language 1

OUTPUT 2: This is to demonstrate the Else IF statement. We entered Totalmarks = 490 means the first IF condition is FALSE. It will check the (Totalmarks >= 480), which is TRUE, so that the Program will print the code inside this block. Although else if(Totalmarks >= 400) condition is TRUE, the compiler will not check this condition.

Please Enter your Total Marks
You are eligible for 50 Percent Scholarship

3rd OUTPUT: Total marks as 401 – first IF condition, else if (Totalmarks >= 480) is FALSE. Next, It will check the else if (Totalmarks >= 401), which is TRUE. So it will print this code block.

Please Enter your Total Marks
You are eligible for 10 Percent Scholarship

OUTPUT 4: Let me enter Total marks = 380. It indicates all the IF conditions Fail. So, It prints the statements inside an else block.

Please Enter your Total Marks
You are Not eligible for Scholarship
We are really Sorry for You