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

C++ if-else Statement

if-else is a fork in the road: if the condition is true you go one way, and otherwise you go the other. One of the two always runs.
Syntax
cpp
if (condition) {
  // runs when condition is true
} else {
  // runs when condition is false
}

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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare age and set it to 15
	int age = 15;
	// Check whether age >= 18
	if (age >= 18) {
		// Print to the console with cout
		std::cout << "Adult" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Minor" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	// Declare day and set it to 6
	int day = 6;
	std::string label;
	// Check whether day == 6 || day == 7
	if (day == 6 || day == 7) {
		label = "weekend";
	} else {
		label = "weekday";
	}
	// Print to the console with cout
	std::cout << label << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to false
	bool isLoggedIn = false;
	// Check whether isLoggedIn
	if (isLoggedIn) {
		// Print to the console with cout
		std::cout << "Welcome back" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Please log in" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare condition and set it to true
	bool condition = true;
	// Check whether condition
	if (condition) {
		// Declare x and set it to 5
		int x = 5;
		// Print to the console with cout
		std::cout << x << std::endl;
	} else {
		int x = 10;
		// Print to the console with cout
		std::cout << x << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Declare hasTicket and set it to true
	bool hasTicket = true;
	// Check whether age >= 18 && hasTicket
	if (age >= 18 && hasTicket) {
		// Print to the console with cout
		std::cout << "Entry allowed" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Entry denied" << 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. Putting a semicolon after the if condition, which ends the if and leaves the else without a partner.
  2. Omitting braces so the wrong statement attaches to the else (dangling else).
  3. Writing else (x > 5) expecting a condition, when a plain else takes none.

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.