if else if statements in C Programming

if else if statements in C Programming

02 May 2024
Beginner
198 Views
7 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

if else...if Statements: An Overview

if else...if statement is a part of a conditional statement in C. It is an extension of the if...else statement. If you want to check multiple conditions if the first “if” condition becomes false, use the if else...if ladder. If all the if conditions become false the else block gets executed. 

In this C Tutorial, we will explore more about the if else...if statement in C Programming which will include the if else...if statement in C example, if else...if statement in  C syntax, and if else...if statement in C flowchart

Syntax of if-else-if statement


if(test condition1){ 
//code to be executed if condition1 is true 
}else if(test condition2){ 
//code to be executed if condition2 is true 
} 
else if(test condition3){ 
//code to be executed if condition3 is true 
} 
... 
else{ 
//code to be executed if all the conditions are false 
} 

Flowchart:

if-else-if statement flowchart

Example

Let's elaborate on the if-else-if statement in the C Compiler.
 #include <stdio.h> 
int main(){ 
int number=5; 
if(number==20){ 
printf("number is equals to 20"); 
} 
else if(number==50){ 
printf("number is equal to 50"); 
} 
else if(number==100){ 
printf("number is equal to 100"); 
} 
else{ 
printf("number is not equal to 20, 50 or 100"); 
} 
return 0; 
} 

Output

number is not equal to 20, 50 or 100

Explanation

  • In the given code, the program evaluates multiple conditions.
  • If the first if condition becomes false, the control goes to the else-if statements.
  • If all the conditions fail, the statement inside the else block gets printed.

How to use If-Else If Statements in C?

If-Else If statement allows us to execute different blocks of code based on various conditions. 
How to use If-Else If Statements in C?
1. The If Statement: This stage has its first condition. If the condition is true in the first step, the block of code will execute.
2. The Else If Statement: If the condition is false in this second stage, the program flow will go to the else if statement, and again it checks the condition. If the condition in the else if statement is true, the block of code within it will execute directly.
3. The Else Statement: If both of the conditions specified in the if and else if statements are true, the code block within the Else statement will execute directly.

Handling Multiple Conditions

In the following example, we will see how to handle multiple conditions in an if-else-if statement.

Example:

 #include 

int main() {
    int No= 10;

    if (No> 0 && No% 2 == 0) {
        printf("Number is positive and even\n");
    }
    else if (No> 0 && No% 2 != 0) {
        printf("Number is positive and odd\n");
    }
    else if (No< 0) {
        printf("Number is negative\n");
    }
    else {
        printf("Number is zero\n");
    }

    return 0;
}

Explanation:

  • So in this example suppose If No is greater than 0 and divisible by 2 with no remainder (No% 2 == 0), it prints "Number is positive and even".
  • In the second condition, If No is greater than 0 and not divisible by 2 (No % 2 != 0), it prints "Number is positive and odd".
  • In the third condition, If the No is less than 0, it prints "Number is negative".
  • In the last condition, If No is 0, it prints "Number is zero".
  • For this kind of program, You can use the && operator as the logical AND operator, Because it checks if both conditions on its left and right sides are true.
  • For that, You can use multiple conditions within the same if or else if statement.

Advantages 

  • Multiple Decision-Making: It allows us to manage multiple conditions in a structured way.
  • Efficient: Using if else if allows the program to skip the operation of particular conditions once a true condition is found, Because of this it improves the efficiency of the code, especially if there are various conditions.
  • Readable: if-else if statements make the code more simple and easy to read.

Disadvantages

  • Complex in nature: As the multiple conditions increase, the if-else-if ladder can become complex and difficult to understand.
  • High Maintenance: Adding or modifying multiple conditions requires careful consideration to ensure that the logic remains correct and all possible scenarios are accounted for which can increase its maintenance.
  • More Execution: Each condition in an if-else if must be executed sequentially until a true condition is found. In short more conditions mean more execution.
Conclusion

So we have explored the if-else-if statement in C. I hope you enjoyed learning these concepts while programming with C. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in basics in C with our C Programming Course.

FAQs

Q1. What is the if-else if statement?

It is used to execute both the true part and the false part of a given condition

Q2. Why use if-else instead of if?

ELSE IF is the more efficient way because the computer only has to check conditions until it finds a condition that returns the value true.

Q3. What is called if-else?

If-else is nothing but control-flow statements as they control the flow of the program.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification Course May 25 SAT, SUN
Filling Fast
06:00PM to 08:00PM (IST)
Get Details
Full Stack .Net Certification Training May 26 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training May 26 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Data Structures and Algorithms Training Jun 04 TUE, FRI
Filling Fast
08:30PM to 10:00PM (IST)
Get Details
Azure DevOps Certification Training Course Jun 09 SUN
Filling Fast
04:30PM to 06:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 21 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
Azure Developer Certification Training Jun 23 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this