TExcelTAD
Home › C++ › C++ Booleans

C++ Booleans

Programs constantly need yes/no answers: Is the user logged in? Is the list empty? Is the number even? C++ represents these answers with the bool type, which has exactly two values: true and false.

Boolean values

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    bool isRaining = false;
    bool hasUmbrella = true;
    cout << isRaining << " " << hasUmbrella << "\n";
    cout << boolalpha << isRaining << " " << hasUmbrella << "\n";
}
0 1
false true

By default cout prints bool values as 1 and 0. The boolalpha manipulator switches to the words true and false (and noboolalpha switches back).

Boolean expressions

Most of the time you do not write true or false yourself — you get them from comparisons and logical operators:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    int temperature = 31;
    int humidity = 80;

    bool hot = temperature > 30;
    bool humid = humidity >= 70;
    bool uncomfortable = hot && humid;
    bool pleasant = !hot && !humid;

    cout << boolalpha;
    cout << "hot: " << hot << "\n";
    cout << "uncomfortable: " << uncomfortable << "\n";
    cout << "pleasant: " << pleasant << "\n";
}
hot: true
uncomfortable: true
pleasant: false

Giving a boolean expression a well-chosen name, like uncomfortable above, often makes code read almost like English.

Truthiness: numbers as conditions

C++ converts other types to bool when a condition needs one:

Value Converts to
0, 0.0 false
any other number true
nullptr false
a non-null pointer true

Going the other way, true becomes 1 and false becomes 0 in arithmetic. That allows a neat counting trick:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    int scores[] = {55, 72, 90, 40, 68};
    int passed = 0;
    for (int s : scores) {
        passed += (s >= 60);   // adds 1 when true, 0 when false
    }
    cout << passed << " students passed\n";

    int items = 0;
    if (!items) {
        cout << "Cart is empty\n";
    }
}
3 students passed
Cart is empty

Tip: Even though if (!items) works, if (items == 0) states your intent more clearly for numbers. Reserve the short form for real bool variables and pointers.

Functions that return bool

Functions that answer a question are usually named isSomething or hasSomething and return bool. You will learn to write functions properly in a later chapter, but here is a preview:

cpp · try it yourself
#include <iostream>
using namespace std;

bool isEven(int n) {
    return n % 2 == 0;
}

bool isLeapYear(int y) {
    return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}

int main() {
    cout << boolalpha;
    cout << isEven(10) << " " << isEven(7) << "\n";
    cout << isLeapYear(2024) << " " << isLeapYear(1900) << " " << isLeapYear(2000) << "\n";
}
true false
true false true

Test yourself

What does cout << (5 > 3); print by default?

  1. true
  2. 1
  3. yes
  4. 5

Which value converts to false in a condition?

  1. -1
  2. 0
  3. 42
  4. 0.5

What is true + true in C++?

  1. true
  2. 2
  3. 1
  4. Compile error