Else if Statement in C Programming - With Some Examples

Else if Statement in C Programming - With Some Examples
Techiio-author
Written by Shuvhojit DebFebruary 10, 2022
8 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, you'll learn the else if statement in C.

Introduction of Else if Statement

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.

Else if Statement in C Syntax & Flowchart

Syntax:

if(condition 1)
{
statement 1;
}
elseif(condition 2)
{
statement 2;
}
elseif(condition 3(
{
statement 3;
}
...
else
{
statement 4;
}

Flowchart:

blogpost

Examples of Else if Statement

  • Example

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 an 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 store these two values respectively.

First, it checks whether two values are equal or not. If the values are equal it will print both the 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:

blogpost

  • Example

Program to calculate the grade using elseif 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:

blogpost

Explanation of the code:

Here we have written a program to calculate the grade using an else if statement. First, we have declared a 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 the 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 a value more than 35. If the student enters a value less than 35 then using the else statement it will print student has failed in the exam using else statement.

C Language
Else if
Else if Statement
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance