← Back to C Course | Chapter 4: Control Flow | Lesson 3 of 11

C else-if Ladder

An else-if ladder asks questions in order until one is true, like grading a test from A down to F. C runs the first match and skips the rest.
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Ordering conditions from broad to specific, such as score >= 50 before score >= 90, so the first true test always wins.
  2. Writing elseif or else-if instead of else if, which is not valid C.
  3. Using separate if statements instead of a ladder, so more than one branch can run for the same value.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.