2.4 — True or false (bool)
Read one box at a time. No rush.
bool#
A bool holds only two values: true or false.
C++
bool isRaining { true };bool hasKey { false };Questions give bools#
When you compare things, you get a bool back:
C++
int age { 15 };bool canDrive { age >= 17 }; // falsePrinting bools#
By default, true prints as 1 and false as 0. Use std::boolalpha to print words:
C++
std::cout << true << '\n';std::cout << std::boolalpha << true << '\n';Output
1trueGood habit
Name bools like yes/no questions: isReady, hasWon, canJump. The code reads like English: if (hasWon).
We will use bools a lot in 4.1 — if: make a choice.
Tip: press ← / → to move between lessons, / to search.