Nested if...else Statement
Introduction to C
Structure of a C Program
Variables and Data Types
Input and Output
Operators in C
Decision Making
If Statement
if...else Statement
Nested if Statement
Nested if...else Statement
Switch Statement
Loops
For Loop
While Loop
Do While Loop
Loop Comparison
Arrays
Strings
Functions
Function Types
Library Functions
User-Defined Functions
Recursion Function
Pointers
Structures
File Handling
Common Mistakes
Ideas
Nested if...else Statement
The if...else if Ladder
What is an Else-If Ladder?
The else-if ladder is used when you need to check multiple conditions in a specific order. It is an extension of the basic if...else statement.
The program starts at the top and checks each condition. As soon as it finds one that is True, it executes that block and then jumps to the very end of the entire ladder, skipping all remaining checks. If no conditions are true, the final else block (if present) is executed.
Syntax
if (condition1) {
// Runs if condition1 is true
} else if (condition2) {
// Runs if condition1 is false AND condition2 is true
} else if (condition3) {
// Runs if prior conditions are false AND condition3 is true
} else {
// Runs if ALL above conditions are false
}
// Runs if condition1 is true
} else if (condition2) {
// Runs if condition1 is false AND condition2 is true
} else if (condition3) {
// Runs if prior conditions are false AND condition3 is true
} else {
// Runs if ALL above conditions are false
}
Practical Examples
Example 1: Student Grading System
int score = 85; if (score >= 90) { printf("Grade: A\n"); } else if (score >= 80) { printf("Grade: B\n"); } else if (score >= 70) { printf("Grade: C\n"); } else { printf("Grade: F\n"); }
Example 2: Number Categorization
int num = -5; if (num > 0) { printf("Positive Number\n"); } else if (num < 0) { printf("Negative Number\n"); } else { printf("The number is Zero\n"); }
Performance Tip: Always place the most likely condition at the top of the ladder. This ensures the program finds a match faster and saves processing time!
🏋️ Test Yourself With Exercises
Take our quiz on Nested if...else Statement to test your knowledge.
Browse Quizzes »