Avinash Singh Programming,C Programming if-else-if Conditional Statement in C Programming

if-else-if Conditional Statement in C Programming

if-else-if Conditional Statement in C Programming post thumbnail image

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

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.

q? encoding=UTF8&ASIN=9391392997&Format= SL250 &ID=AsinImage&MarketPlace=IN&ServiceVersion=20070822&WS=1&tag=educationandi 21&language=en INir?t=educationandi 21&language=en IN&l=li3&o=31&a=9391392997

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.

MarksResult
<33Fail
< 45Thrid
< 60Second
>=60First

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.

#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

You can also visit my Hindi YouTube Channel.

mm
Avinash Singh

I am Avinash Singh, a software engineer from the profession and have been working in the IT industry since 2009. I live in Barabanki district of Uttar Pradesh in India and completed intermediate in 2001 from Ram Sevak Inter College, a UP Board school. After inter I completed my professional degree (BCA & MCA) from Indira Gandhi National Open University, Delhi in 2008 and along with it I did many certifications to keep my knowledge up to date and the next year I started a software development company E – Vision Technocraft Pvt Ltd.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post