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

C++ Logical Conditions

Logical operators (&&, ||, !) combine multiple individual conditions into a single expression an if statement can test, using short-circuit evaluation to skip unnecessary checks.
Syntax
cpp
if (condition1 && condition2) {
  // both are true
}

if (condition1 || condition2) {
  // at least one is true
}

if (!condition) {
  // condition is false
}

Logical Operators in Conditions

Logical operators combine multiple individual conditions into a single expression that an if statement can test, letting a decision depend on more than one factor at once.

Example: Logical Operators in 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 hasID and set it to true
	bool hasID = true;
	// Check whether age >= 18 && hasID
	if (age >= 18 && hasID) {
		// Print to the console with cout
		std::cout << "Allowed" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Combining Conditions with &&

The && (logical AND) operator combines two conditions so the overall expression is true only when both individual conditions are true, commonly used to check that a value falls within a specific range.

Example: Combining Conditions with &&

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

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

Combining Conditions with ||

The || (logical OR) operator combines two conditions so the overall expression is true when at least one of them is true, useful for checking whether a value matches any of several acceptable options.

Example: Combining Conditions with ||

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

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

Negating a Condition with !

The ! (logical NOT) operator inverts a single condition, turning a true value false and a false value true, which is often used to read naturally as not in front of a condition's name.

Example: Negating a Condition with !

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

// Program execution starts in main()
int main() {
	// Declare isRaining and set it to false
	bool isRaining = false;
	// Check whether !isRaining
	if (!isRaining) {
		// Print to the console with cout
		std::cout << "Go outside" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Short-Circuit Evaluation

C++ evaluates && and || using short-circuit evaluation: for &&, if the first condition is false, the second is never evaluated at all, which is commonly used to safely guard against operations like division by zero.

Example: Short-Circuit Evaluation

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

bool sideEffect() {
	// Print to the console with cout
	std::cout << "Second condition evaluated" << std::endl;
	// Send true back to the caller
	return true;
}

// Program execution starts in main()
int main() {
	// Declare first and set it to false
	bool first = false;
	// Check whether first && sideEffect()
	if (first && sideEffect()) {
		// Print to the console with cout
		std::cout << "Both true" << std::endl;
	}
	std::cout << "Done" << 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. Writing x > 1 && < 10 and leaving out the second x, which does not compile.
  2. Confusing && with ||, so a condition that should require both checks passes on either.
  3. Forgetting parentheses when mixing && and ||, since && binds tighter than ||.

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.