The Else If Statement in C is instrumental while we have to test several conditions. Apart from Else If Statement in C, we can utilize the Nested If statement to accomplish the same. However, as the total number of conditions rises, the code complexity will further grow.
Else If statement in C effectively handles multiple statements by sequentially executing them. Javac 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, Javac verifies the Next one (Else If condition) and so on.
Else If Statement in C Syntax
The syntax of Else If statement in C Programming 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 else default statements
There are some circumstances where both the condition1, condition2 are TRUE, for instance:
x= 20, y=10
Condition 1: x > y //TRUE
Condition 2: x != y //TRUE
In these circumstances, code under the Condition1 will execute. Because ELSE IF conditions will only execute if it’s prior IF or ELSE IF statement fails.
Else If Statement in C Flow chart
The programming flow Chart behind this Else If Statement in C Programming is as shown below
Else If Statement in C Example
In this C else if 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 scholarship or not
/* Example for Else If Statement in C Language */ #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"); printf("Congratulations\n"); } else if (Totalmarks >= 480) { printf("You are eligible for 50 Percent Scholarship\n"); printf("Congratulations\n"); } else if (Totalmarks >= 400) { printf("You are eligible for 10 Percent Scholarship\n"); printf("Congratulations\n"); } else { printf("You are Not eligible for Scholarship\n"); printf("We are really Sorry for You\n"); } }
OUTPUT 1: For this Else if in c example, we are going to provide Totalmarks = 570. Here first If condition is TRUE. Please refer to the C Nested If statement article as well.
OUTPUT 2: This is to demonstrate the C Else IF statement. We entered Totalmarks = 490 means the first IF condition is FALSE. It will check the else if (Totalmarks >= 480), which is TRUE so that C Programming will print the statements inside this block. Although else if (Totalmarks >= 400) condition is TRUE, but Javac will not check this condition.
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 these statements.
OUTPUT 4: Let me enter Total marks = 380. It indicates all the IF conditions Fail. So, It prints the statements inside an else block.