Introduction on Else if Statement in C
Else if the statement is a control statement in C language. Else if the statement is quite similar to an if-else statement, the only difference is if-else statement is used when one or two choice needs to be evaluated while else if the statement is useful when there is a need for a multipath decision. This statement is also called as else if ladder as it looks like else if ladder structure. In this article, we are going to discuss the working of else if statement in C language with the help of examples.
Syntax
if(condition 1)
{
statement 1;
}
elseif(condition 2)
{
statement 2;
}
elseif(condition 3(
{
statement 3;
}
...
else
{
statement 4;
}
Flowchart of Else if Statement in C
Flowchart of else if statement in C is given as follows:
How Else if Statement works in C Language?
elseif keyword is used to perform the else if function in C language. First, if the function is used to check the condition. If the condition mentioned in the if the statement is true, then statement following the if will execute. If the condition mentioned in the of else statement is false, then the flow control is transferred to the else if statement. It again checks the condition mentioned in the elseif statement; if the condition is true, it will execute the statement following respective else if Statement. If the condition is false, it will transfer th control to next.
It follows the same until all conditions tested, execute the appropriate statement. If all condition is false, it executes the else statement. Let us take the sample of syntax. If the condition is true, then it will execute the statement 1 else, it checks the condition 2. If condition 2 is true it will execute statement 2 else it transfers control to condition 3. If condition 3 is true it will execute statement 3 else it transfers to another condition. The flow continues until all conditions are tested and execute the respective one. If all the condition is false, then it will transfer control to the else statement and execute the statement 4.
Examples of else if Statement in C
Examples of else if Statement in C are given as follows:
Example #1
Program to compare two values using else if Statement in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2;
printf("Enter first value: ");
scanf("%d",&num1);
printf("Enter second value: ");
scanf("%d",&num2);
if(num1 ==num2)
{
printf("Both first and second value is equal");
}
else if(num1 > num2)
{
printf("First value is greater than second value");
}
else
{
printf("First value is smaller than second value");
}
return 0;
}
Explanation of the code:
Here we have written a program to compare two values using else if statement. First, we have declared two numbers num1 and num2. Scanf is used to allow a user to enter the two values according to their wish when a user enters these two values num1 and num2 stores these two values respectively.
First, it checks whether two values are equal or not. If the values are equal it will print both first and second value is equal. If not then it checks the second condition. The second condition checks whether num1 is greater than num2. If yes then it prints first value is greater than the second value. If no, it will execute the else statement and simply print the first value smaller than the second value.
Output:
Example #2
Program to calculate the grade using else if statement in C
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int percentage;
printf("Enter the percentage:");
scanf("%d",& percentage);
if(percentage > 75)
{
printf("Congrats! You passed with first class");
}
else if(percentage == 75)
{
printf("Congrats! You passed with distinction");
}
else if(percentage < 75 && percentage > 65)
{
printf("Congrats! You passed with second class");
}
else if(percentage < 65 && percentage > 55)
{
printf("Congrats! You passed with third class");
}
else if(percentage < 55 && percentage > 45)
{
printf("Congrats! You passed with fourth class");
}
else if(percentage < 45 && percentage >= 35)
{
printf("Congrats! You have cleared the exam");
}
else
{
printf("You failed to clear the exam");
}
return 0;
}
Output:
Explanation of the code:
Here we have written a program to calculate the grade using elseif statement. First, we have declared value called percentage. Scanf is used to allow a user to enter the value according to their wish. When a user enters the value, percentage stores an input. First, it checks whether the input is greater than 75. If the values are greater than 75, then it will print student has passed with first class. If not then it checks the second condition.
The second condition checks whether the input is equal to 75. If the values are equal to 75, then it will print student has passed with distinction. If this also does not satisfy the condition then it will check the third condition. The third condition checks whether the input is less than 75 or greater than 65. If the values are less than 75 or greater than 65, then it will print student has passed with second class.
If this also does not satisfy the condition then it will check the fourth condition. The third condition checks whether an input is less than 65 or greater than 55. If the values are less than 75 or greater than 65, then it will print student has passed with third class. Hence loop continues until the student enters the value more than 35. If the student enters a value less than 35 then using else statement it will print student has failed in the exam using else statement.
Conclusion
In this article, we have seen how to use else if statement to execute multiple conditions in the same program along with some examples.
Recommended Articles
This is a guide to the Else if Statement in C. Here we discuss the introduction, how Else if Statement works in C language, and respective examples and flowchart. You may also have a look at the following articles to learn more –