C++ Boolean Expressions
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
x = 5inside a condition when you meanx == 5. - Expecting
1 < x < 10to test a range, when it is evaluated as(1 < x) < 10and is always true. - Assuming a non-bool value must be compared, when any non-zero number in an expression counts as true.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: