C else-if Ladder
In this page:
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}
What is an else-if Ladder?
An else-if ladder chains multiple conditions together so they're checked in order, and as soon as one evaluates to true, its block runs and every later condition in the chain is skipped without being checked at all.
Example: What is an else-if Ladder?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 90
if (score >= 90) {
// Print formatted text to the screen
printf("A");
} else if (score >= 70) {
// Print formatted text to the screen
printf("B");
} else {
printf("C");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Grading System
Assigning letter grades based on numeric score ranges is a textbook use of an else-if ladder -- each condition tests a lower boundary, and because they're checked in order, only the first matching range applies.
Example: Grading System
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 82
int score = 82;
// Check whether score >= 90
if (score >= 90) {
// Print formatted text to the screen
printf("A");
} else if (score >= 80) {
// Print formatted text to the screen
printf("B");
} else if (score >= 70) {
printf("C");
} else {
printf("F");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Number Classification
You can classify a number as positive, negative, or zero with three chained conditions in an else-if ladder, which reads far more clearly than three separate, unrelated if statements checking the same variable.
Example: Number Classification
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to -5
int n = -5;
// Check whether n > 0
if (n > 0) {
// Print formatted text to the screen
printf("Positive");
} else if (n < 0) {
// Print formatted text to the screen
printf("Negative");
} else {
printf("Zero");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Age Categories
Grouping ages into categories like child, adult, and senior with an else-if ladder keeps the boundaries between categories explicit and easy to adjust later, since each range is defined in one place.
Example: Age Categories
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 45
int age = 45;
// Check whether age < 13
if (age < 13) {
// Print formatted text to the screen
printf("Child");
} else if (age < 65) {
// Print formatted text to the screen
printf("Adult");
} else {
printf("Senior");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Temperature Guides
Classifying temperature ranges (cold, mild, hot) is a natural fit for an else-if ladder, since each condition only needs to check one boundary given that earlier, more extreme ranges have already been ruled out by prior checks.
Example: Temperature Guides
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare temp and set it to 85
int temp = 85;
// Check whether temp < 50
if (temp < 50) {
// Print formatted text to the screen
printf("Cold");
} else if (temp < 80) {
// Print formatted text to the screen
printf("Mild");
} else {
printf("Hot");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Ordering conditions from broad to specific, such as
score >= 50beforescore >= 90, so the first true test always wins. - Writing
elseiforelse-ifinstead ofelse if, which is not valid C. - Using separate
ifstatements instead of a ladder, so more than one branch can run for the same value.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: