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

C++ if Statement

An if statement runs code only when a condition is true, like taking a coat only if it is cold. If it is false, C++ skips it.
Syntax
cpp
if (condition) {
  // runs when condition is true
}

Basic if Statement

An if statement evaluates a condition and executes its associated block only when that condition is true; when it's false, the block is skipped entirely and execution continues right after it.

This is the most fundamental building block for making a program's behavior depend on data rather than always doing the same thing.

Example: Basic if Statement

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

// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Check whether age >= 18
	if (age >= 18) {
		// Print to the console with cout
		std::cout << "Adult" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Comparison inside if Condition

Comparison operators like == inside an if condition let you branch based on whether two values match, such as if (userChoice == y).

Be careful not to accidentally type a single = here, since that would assign instead of compare and would almost always evaluate to true regardless of the actual values.

Example: Comparison inside if Condition

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

// Program execution starts in main()
int main() {
	// Declare userChoice and set it to 'y'
	char userChoice = 'y';
	// Check whether userChoice == 'y'
	if (userChoice == 'y') {
		// Print to the console with cout
		std::cout << "Confirmed" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical AND in if Conditions

Combining two conditions with && inside an if requires both to be true before the block runs, such as if (age >= 18 && hasLicense), which only grants access when every requirement is satisfied simultaneously.

This is how you express 'all of these things must hold' in code.

Example: Logical AND in if Conditions

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

// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Declare hasLicense and set it to true
	bool hasLicense = true;
	// Check whether age >= 18 && hasLicense
	if (age >= 18 && hasLicense) {
		// Print to the console with cout
		std::cout << "Can drive" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical OR in if Conditions

Using || instead lets the block run if at least one condition is true, such as if (isWeekend || isHoliday), useful whenever any single qualifying condition should be enough to proceed.

Mixing && and || in one condition often needs parentheses to make the intended grouping unambiguous.

Example: Logical OR in if Conditions

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

// Program execution starts in main()
int main() {
	// Declare isWeekend and set it to false
	bool isWeekend = false;
	// Declare isHoliday and set it to true
	bool isHoliday = true;
	// Check whether isWeekend || isHoliday
	if (isWeekend || isHoliday) {
		// Print to the console with cout
		std::cout << "No work today" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested if Statements

Placing one if inside another lets you check a second condition only once the first has already passed, such as verifying a user is logged in before separately checking whether they have admin rights.

This mirrors real decision-making, where some questions only make sense to ask after an earlier one has already been answered.

Example: Nested if Statements

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

// Program execution starts in main()
int main() {
	// Declare isLoggedIn and set it to true
	bool isLoggedIn = true;
	// Declare isAdmin and set it to true
	bool isAdmin = true;
	// Check whether isLoggedIn
	if (isLoggedIn) {
		// Check whether isAdmin
		if (isAdmin) {
			// Print to the console with cout
			std::cout << "Welcome, admin" << 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. Using = instead of == in the condition, like if (x = 5).
  2. Adding a semicolon right after the condition, if (x > 5);, so the block always runs.
  3. Omitting braces on a multi-line body, so only the first statement belongs to the if.

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.