Lesson 58 +10 XP

Boolean Values

Boolean Values

A boolean is a value that is either true or false. C++ calls the type bool, named after George Boole.

Declaring a boolean

bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << endl;  // 1
cout << isFishTasty << endl;  // 0

Why 1 and 0?

Booleans have their own type, but when printed they show up as numbers: true becomes 1, and false becomes 0. They are really just on/off switches inside the computer.

Boolean keywords

  • true means the switch is on.
  • false means the switch is off.
  • You write them in lowercase.
  • A boolean variable holds exactly one of them.

TL;DR

  • Use type bool for true/false values.
  • Write true or false in lowercase.
  • Printing a bool shows 1 for true and 0 for false.