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

C++ else-if Ladder

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 <iostream>

int main() {
	int score = 75;
	if (score >= 90) {
		std::cout << "A" << std::endl;
	} else if (score >= 70) {
		std::cout << "B" << std::endl;
	} else {
		std::cout << "C" << std::endl;
	}
	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 <iostream>

int main() {
	int score = 65;
	if (score >= 90) {
		std::cout << "A" << std::endl;
	} else if (score >= 80) {
		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;
}

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 <iostream>

int main() {
	int score = 95;
	if (score >= 60) {
		std::cout << "Pass (matches too early)" << std::endl;
	} else if (score >= 90) {
		std::cout << "A (never reached)" << std::endl;
	}
	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 <iostream>

int main() {
	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;
}

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 <iostream>

int main() {
	int score = 40;
	if (score >= 90) {
		std::cout << "A" << std::endl;
	} else if (score >= 70) {
		std::cout << "B" << std::endl;
	} else {
		std::cout << "Fail" << std::endl;
	}
	return 0;
}

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.