← Back to C++ Course | Chapter 4: Control Flow | Lesson 5 of 13

C++ else-if Ladder

An else-if ladder asks questions in order until one is true, like checking the weather: sunny, cloudy, rainy? C++ runs the first match and skips the rest.
Syntax
cpp
if (condition1) {
  // block 1
} else if (condition2) {
  // block 2
} else {
  // default block
}

Basic else-if Ladder

An else-if ladder checks a sequence of conditions from top to bottom and runs only the block belonging to the first one that evaluates to true, skipping every condition after it.

This makes it the natural choice whenever you have several mutually exclusive possibilities to test in order, like assigning a letter grade based on a numeric score.

Example: Basic else-if Ladder

cpp
// Include std::cout and std::cin
#include <iostream>

// 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 to the console with cout
		std::cout << "A" << std::endl;
	} else if (score >= 70) {
		// Print to the console with cout
		std::cout << "B" << std::endl;
	} else {
		std::cout << "C" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple else-if Blocks

You can chain as many else if blocks as needed after the initial if, which scales well for checking many distinct categories or numeric ranges without deeply nested if statements.

Each additional else if only gets evaluated if every condition above it was false.

Example: Multiple else-if Blocks

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 65
	int score = 65;
	// Check whether score >= 90
	if (score >= 90) {
		// Print to the console with cout
		std::cout << "A" << std::endl;
	} else if (score >= 80) {
		// Print to the console with cout
		std::cout << "B" << std::endl;
	} else if (score >= 70) {
		std::cout << "C" << std::endl;
	} else if (score >= 60) {
		std::cout << "D" << std::endl;
	} else {
		std::cout << "F" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Order of Conditions matters

Because the ladder stops at the first true condition, ordering matters enormously — placing a broad condition like score >= 60 before a narrower one like score >= 90 would incorrectly catch high scores in the wrong branch.

Always order conditions from most specific to most general when ranges overlap.

Example: Order of Conditions matters

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 95
	int score = 95;
	// Check whether score >= 60
	if (score >= 60) {
		// Print to the console with cout
		std::cout << "Pass (matches too early)" << std::endl;
	} else if (score >= 90) {
		// Print to the console with cout
		std::cout << "A (never reached)" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Ladder vs Single Ifs

A single else-if ladder guarantees at most one block runs, since evaluation stops the moment a true condition is found, whereas writing several separate, independent if statements would test every condition regardless and could execute more than one block.

Choose a ladder specifically when the branches represent mutually exclusive outcomes.

Example: Ladder vs Single Ifs

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 95
	int score = 95;

	if (score >= 90) std::cout << "A" << std::endl;
	else if (score >= 70) std::cout << "B" << std::endl;

	if (score >= 90) std::cout << "Also true (separate check)" << std::endl;
	if (score >= 70) std::cout << "Also true (separate check)" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Default Fallback with Else

A trailing else with no condition acts as the ladder's catch-all, running only if every single condition above it turned out false — this is the natural place to handle unexpected or invalid input.

Omitting it means that if none of the conditions match, the program simply does nothing at all, which can silently hide bugs.

Example: Default Fallback with Else

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare score and set it to 40
	int score = 40;
	// Check whether score >= 90
	if (score >= 90) {
		// Print to the console with cout
		std::cout << "A" << std::endl;
	} else if (score >= 70) {
		// Print to the console with cout
		std::cout << "B" << std::endl;
	} else {
		std::cout << "Fail" << std::endl;
	}
	// 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, like score >= 50 before score >= 90, so the first test always wins.
  2. Writing else if as elseif, which is not valid C++.
  3. Using separate if statements instead of a ladder, so more than one branch can run.

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.