if else if statements in C Programming

if else if statements in C Programming

02 May 2024
Beginner
115 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 Training Batches Schedule
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
  • 750+ Hands-On Labs
  • 300+ 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