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

C++ if-else Statement

Introduction to if-else

An if-else statement gives you exactly two paths: the if block runs when the condition is true, and the else block runs whenever it's false, guaranteeing that exactly one of the two always executes. This is more useful than a bare if alone whenever you have a specific fallback action rather than simply doing nothing.

Example: Introduction to if-else

cpp
#include <iostream>

int main() {
	int age = 15;
	if (age >= 18) {
		std::cout << "Adult" << std::endl;
	} else {
		std::cout << "Minor" << std::endl;
	}
	return 0;
}

Variable Assignment inside if-else

Using if-else to set a variable's value based on a condition, like choosing between a weekday or weekend label, lets that decision live right where the variable is defined rather than scattered through later code. This pattern is common enough that C++ also offers the ternary operator as a more compact alternative for simple two-way value choices.

Example: Variable Assignment inside if-else

cpp
#include <iostream>
#include <string>

int main() {
	int day = 6;
	std::string label;
	if (day == 6 || day == 7) {
		label = "weekend";
	} else {
		label = "weekday";
	}
	std::cout << label << std::endl;
	return 0;
}

Boolean Flags in if-else

Boolean variables pair naturally with if-else because the condition itself already reads like a question — if (isLoggedIn) is immediately clear, unlike checking a numeric flag against 0 or 1. This is exactly the pattern behind common features like login gates and subscription-only content.

Example: Boolean Flags in if-else

cpp
#include <iostream>

int main() {
	bool isLoggedIn = false;
	if (isLoggedIn) {
		std::cout << "Welcome back" << std::endl;
	} else {
		std::cout << "Please log in" << std::endl;
	}
	return 0;
}

Block Scope inside if-else

A variable declared inside an if or else block's curly braces only exists for the duration of that block and disappears once execution leaves it — attempting to reference it afterward causes a compile error. This scoping rule prevents variables meant for one branch's temporary logic from accidentally leaking into or colliding with code elsewhere.

Example: Block Scope inside if-else

cpp
#include <iostream>

int main() {
	bool condition = true;
	if (condition) {
		int x = 5;
		std::cout << x << std::endl;
	} else {
		int x = 10;
		std::cout << x << std::endl;
	}
	return 0;
}

Compound Conditions in if-else

Combining several conditions with && and || inside a single if-else lets you express fairly sophisticated decision logic in one statement, such as granting access only to logged-in admins or verified moderators. As these compound conditions grow more complex, breaking them into clearly named boolean variables first often keeps the final if-else far more readable.

Example: Compound Conditions in if-else

cpp
#include <iostream>

int main() {
	int age = 25;
	bool hasTicket = true;
	if (age >= 18 && hasTicket) {
		std::cout << "Entry allowed" << std::endl;
	} else {
		std::cout << "Entry denied" << 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.