2.4 — True or false (bool)

📘 Chapter 2⏱ about 1 min read✍️ Beginner friendly
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 };   // false

Printing 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
1true
Good 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.

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.