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

C++ Boolean Expressions

A boolean expression combines values and operators to produce true or false, and C++ implicitly converts any non-bool value used as a condition, with zero treated as false and everything else as true.
Syntax
cpp
bool result = (left_value comparison_operator right_value);
bool result = (condition1) && (condition2);

Building a Boolean Expression

A boolean expression is built from comparison or logical operators applied to values or variables, and the whole expression collapses down to a single true or false result.

Example: Building a Boolean Expression

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

// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// Declare result and set it to (x > 5) && (x < 20)
	bool result = (x > 5) && (x < 20);
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Comparison Operators in Expressions

Comparison operators like ==, !=, <, >, <=, and >= are the most common building blocks of a boolean expression, each comparing two values and producing true or false.

Example: Comparison Operators in Expressions

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

// Program execution starts in main()
int main() {
	int a = 5, b = 10;
	// Print to the console with cout
	std::cout << (a == b) << " " << (a < b) << " " << (a >= b) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Logical Operators in Expressions

Logical operators &&, ||, and ! combine or invert simpler boolean expressions into a single larger expression, letting a condition depend on multiple factors at once.

Example: Logical Operators in Expressions

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

// Program execution starts in main()
int main() {
	bool loggedIn = true, isAdmin = false;
	// Declare canEdit and set it to loggedIn && !isAdmin
	bool canEdit = loggedIn && !isAdmin;
	// Print to the console with cout
	std::cout << canEdit << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Implicit Conversion in Expressions

Any non-bool value used where a boolean expression is expected is implicitly converted: zero (or an empty/null value) converts to false, and anything else converts to true.

Example: Implicit Conversion in Expressions

cpp
#include <iostream>

int main() {
	int count = 0;
	if (count) { // 0 converts to false
		std::cout << "Has items" << std::endl;
	} else {
		std::cout << "Empty" << std::endl;
	}
	return 0;
}

Evaluating Complex Expressions

A boolean expression can combine several comparisons and logical operators together, and understanding operator precedence (comparisons bind tighter than && and ||) is important for getting the intended result.

Example: Evaluating Complex Expressions

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 hasLicense and set it to true
	bool hasLicense = true;
	bool canDrive = age >= 18 && hasLicense || false;
	// Print to the console with cout
	std::cout << canDrive << 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 = 5 inside a condition when you mean x == 5.
  2. Expecting 1 < x < 10 to test a range, when it is evaluated as (1 < x) < 10 and is always true.
  3. Assuming a non-bool value must be compared, when any non-zero number in an expression counts as true.

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.