C++ Booleans
In this page:
bool variable_name = true; // or false
bool variable_name = expression; // e.g. x > 5
The bool Type
The bool type represents exactly one of two possible values, true or false, and is C++'s dedicated type for logical values, used whenever a piece of information is a simple yes-or-no flag.
Example: The bool Type
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isSubscribed and set it to true
bool isSubscribed = true;
// Print to the console with cout
std::cout << isSubscribed << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Expressions
A boolean expression is any expression that evaluates to true or false, most commonly formed by comparing two values with operators like >, <, ==, or != rather than by writing a literal true or false directly.
Example: Boolean Expressions
// 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
bool result = x > 5;
// 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
Booleans in Conditions
Boolean values are what control flow statements like if, while, and for actually test: an if statement runs its block only when the boolean expression inside its parentheses evaluates to true.
Example: Booleans in Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isOpen and set it to true
bool isOpen = true;
// Check whether isOpen
if (isOpen) {
// Print to the console with cout
std::cout << "Store is open" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Variables from Comparisons
Storing the result of a comparison in a named bool variable, rather than repeating the comparison inline, makes conditions easier to read and lets the same computed result be reused in multiple places.
Example: Boolean Variables from Comparisons
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 70
int score = 70;
bool passed = score >= 60;
// Check whether passed
if (passed) {
// Print to the console with cout
std::cout << "Passed" << std::endl;
}
// Print to the console with cout
std::cout << passed << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Booleans with boolalpha
By default cout prints a bool as 1 or 0, but the boolalpha stream manipulator makes it print the words true and false instead, which can be more readable in output.
Example: Printing Booleans with boolalpha
#include <iostream>
int main() {
bool active = true;
std::cout << active << std::endl; // 1
std::cout << std::boolalpha << active << std::endl; // true
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Printing a bool and expecting
true, whencoutprints1unless you useboolalpha. - Reading a bool with
cinand typingtrue, which fails; withoutboolalphait expects 0 or 1. - Assuming only 1 is true, when any non-zero value is true.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: