In this post, we will learn with examples how to use if-else-if conditional statement in C programming.
To understand else if conditional statements, first you need to know about if else conditional statements.
That’s why I would suggest that if you have not read the post with If else conditional statements, then you must read the post thoroughly by clicking on the link given below.
Nested IF Else Statement In C Programming
Contents
- 1 if-else-If Conditional Statement in C Programming
- 2 Difference Between If-else and if-else-If
- 2.1 Print Result Program using if:
- 2.2 Output 1: (Enter Your Marks : 25)
- 2.3 Output 2: (Enter Your Marks : 42)
- 2.4 Output 3: (Enter Your Marks : 72)
- 2.5 Explanation
- 2.6 Print Result Program using Else IF:
- 2.7 Output 1: (Enter Your Marks : 25)
- 2.8 Output 2: (Enter Your Marks : 42)
- 2.9 Output 3: (Enter Your Marks : 70)
- 3 Conclusion
- 4 You can also visit my Hindi YouTube Channel.
if-else-If Conditional Statement in C Programming
When we have to apply different conditions to run different statements in such a way that if one condition is true then the rest of the conditions become false.
That is, all those conditions are connected to each other in such a way that only one condition is true at a time and all the statements depend on that condition only.
For this kind of programming approach, we use if-else-if statement with if in C programming.
if-else-if Syntax:
if(condition1)
{
//These statements would execute if condition1 is true
}
else if(condition2)
{
//These statements would execute if condition2 is true
}
else if(condition3)
{
//These statements would execute if condition3 is true
}
else if(condition N)
{
//These statements would execute if condition N is true
}
else
{
//These statements would execute if all the conditions return false.
//This part is also optional
}
if-else-if Syntax Explanation:
You can also use if statement without else and else if but else or else if cannot be used without if.
With an if statement, else is used only once, but you can use if and if together any number of times.
As you can see in the above syntax, first the condition of the if statement will be checked, if that condition becomes false then one by one the condition of else if will be checked.
If the condition of any else if becomes true, then the subsequent else if and else are automatically assumed to be false i.e. their condition will not be checked at all.
When the if and any other if statement does not run then the else statement is run.
Difference Between If-else and if-else-If
Let us understand with a program example what is the difference between using if-else-if statements with simple if-else and if.
We have to make a program in which we have to take input of marks from the user and print the result to the user on the basis of the conditions given below.
Marks | Result |
---|---|
<33 | Fail |
< 45 | Thrid |
< 60 | Second |
>=60 | First |
First we will make this program with simple if statement and understand why else if is a better option for this type of program.
Print Result Program using if:
#include <stdio.h>
int main()
{
int marks;
printf("Enter Your Marks : ");
scanf("%d",&marks);
if(marks<33)
{
printf("Fail\n");
}
if(marks<45)
{
printf("Third\n");
}
if(marks<60)
{
printf("Second\n");
}
if(marks>=60)
{
printf("First\n");
}
return 0;
}
Output 1: (Enter Your Marks : 25)
Fail
Third
Second
Output 2: (Enter Your Marks : 42)
Third
Second
Output 3: (Enter Your Marks : 72)
Enter Your Marks : 70
First
Explanation
As you know that if and else are connected to each other i.e. when if statements run then else statements will not run and when else if then there will be no if.
But when you write many ifs one below the other, then you may feel that they are connected to each other but in real it does not happen.
All the ifs will be considered separately i.e. after the condition of first if is true, then the condition of second then third and then all subsequent ifs will be checked.
Due to this problem, multiple outputs of the above program are coming. If you want this not to happen, then for this you will use an else if.
Because as I told you above that when you use if with else if then there is a kind of relation in all of them due to which only one condition is true.
Print Result Program using Else IF:
#include <stdio.h>
int main()
{
int marks;
printf("Enter Your Marks : ");
scanf("%d",&marks);
if(marks<33)
{
printf("Fail\n");
}
else if(marks<45)
{
printf("Third\n");
}
else if(marks<60)
{
printf("Second\n");
}
else
{
printf("First\n");
}
return 0;
}
Output 1: (Enter Your Marks : 25)
Fail
Output 2: (Enter Your Marks : 42)
Second
Output 3: (Enter Your Marks : 70)
First
Conclusion
If you want to understand the if-else-if conditional statement better, then definitely understand the programs given below.
Write a program in C Program to Find the Largest of Three Numbers using Nested if-else-if statement