Skip to content

bool

Topic: bool, flow control.

Video:

  • Boolean values (true, false)
  • Boolean expressions
  • Numeric comparison operators >, <, >=, <=, ==, !=
  • Boolean operators &&, ||, ==, !, !=
  • Lazy evaluation of the operands of the && and || operators
  • Implicit conversion to bool from int and pointers
  • The NULL macro
  • The special nullptr value
bool a { true };
std::cout << a << std::endl;
Answer

The bool type has only two possible values: true and false.
When printed to the console, true is displayed as 1 and false as 0.

bool a { 0 };
bool b { 1 };
Answer

You can also store 0 instead of false, and 1 instead of true. This compiles successfully.

bool c { static_cast<bool>(123) };
Answer

static_cast<bool> applied to an int produces true if the number is not 0, otherwise false. c will contain true.

std::cout << sizeof(bool) << std::endl;
Answer

bool takes 1 byte, even though just one bit is enough to represent two values, 0 or 1. If you want to store several bool values in separate bits of the same byte, you need to use bit masks.

bool a = 1 == 2;
std::cout << a << std::endl;
Answer

The == operator takes 2 expressions as operands and returns the result of the equality check as a Boolean value. The result is false.

int x = 3;
int y = 4;
bool b = x == y;
std::cout << b << std::endl;
Answer

Here, the operator works with the values of the variables rather than with the variables themselves. When the code runs, the variables are evaluated to their values before the comparison.

int x = 3;
int y = 4;
bool b = x == y;
x = 4;
std::cout << b << std::endl;
Answer

b stores not the expression x == y, but the result of evaluating it (false). Subsequent changes to x do not affect b in any way.

int x = 3;
int y = 4;
bool b = x * 2 == y + 4;
std::cout << b << std::endl;
Answer

This is an example of using more complex expressions as operands.

bool b = x * 2 == y + 4 is interpreted as bool b = ((x * 2) == (y + 4)). The calculations then proceed according to the rules for expressions.

bool a = 1 > 2;
a = 3 == 3;
std::cout << a << std::endl;
Answer

Here, a is reassigned from false to another value (true).

#include <iostream>
void f(bool a)
{
std::cout << a << std::endl;
}
int main()
{
bool a = true;
f(a);
}

11. Passing a Boolean expression to a function

Section titled “11. Passing a Boolean expression to a function”
#include <iostream>
void f(bool flag)
{
std::cout << flag << std::endl;
}
int main()
{
f(5 > 3);
}
Answer

The argument 5 > 3 is evaluated before the function is called.
flag receives the result true.

12. Function that reassigns a bool parameter

Section titled “12. Function that reassigns a bool parameter”
#include <iostream>
void f(bool b)
{
b = true;
}
int main()
{
int x { 1 };
f(x == 2);
std::cout << x << std::endl;
}
Answer

When f is called, it receives the result of evaluating the expression x == 2. Changing the parameter b does not affect x in any way.

It prints 1.

#include <iostream>
bool f()
{
return true;
}
int main()
{
bool result = f();
std::cout << result << std::endl;
}
#include <iostream>
bool IsGreater(int a, int b)
{
return a > b;
}
int main()
{
bool result = IsGreater(5, 6);
std::cout << result << std::endl;
}
bool a = true;
bool b = false;
bool c = a == b;
std::cout << c << std::endl;
Answer

The equality operator == can be applied to expressions of type bool. The comparison true == false produces false.

bool a = false;
bool b = !a;
std::cout << b << std::endl;
Answer

The ! operator turns false into true (and vice versa).

bool a = true;
bool b = false;
bool c = a && b;
std::cout << c << std::endl;
Answer

a && b -> true && false -> false, because both operands must be true.

#include <iostream>
bool A()
{
std::cout << "A" << std::endl;
return true;
}
bool B()
{
std::cout << "B" << std::endl;
return true;
}
int main()
{
}
Answer

Defining the functions A and B only describes what would happen if they were called. Since main does not call either function, their bodies never execute, so nothing gets printed.

#include <iostream>
bool A()
{
std::cout << "A" << std::endl;
return true;
}
bool B()
{
std::cout << "B" << std::endl;
return true;
}
int main()
{
bool result = A() && B();
}
Answer

To determine whether both A() and B() return true, the program has to call both of them.

A
B
#include <iostream>
bool A()
{
std::cout << "A" << std::endl;
return true;
}
bool B()
{
std::cout << "B" << std::endl;
return false;
}
int main()
{
bool result = A() && B();
}
Answer

The first function returns true, so the second one is also executed.
The second returns false, and the result of the expression is A() && B() -> true && false -> false.

A
B
#include <iostream>
bool A()
{
std::cout << "A" << std::endl;
return false;
}
bool B()
{
std::cout << "B" << std::endl;
return true;
}
int main()
{
bool result = A() && B();
}
Answer

With &&, if the first operand is false, the second is not evaluated.
This is called lazy evaluation.

A
#include <iostream>
bool A()
{
std::cout << "A" << std::endl;
return true;
}
bool B()
{
std::cout << "B" << std::endl;
return true;
}
int main()
{
bool result = A() || B();
}
Answer

With ||, if the first operand is true, the second is not evaluated.
This is also lazy evaluation.

A

23. A function changes a variable used in a parameter expression

Section titled “23. A function changes a variable used in a parameter expression”
#include <iostream>
void func(bool b, int* a)
{
std::cout << b << std::endl;
*a = 2;
std::cout << b << std::endl;
}
int main()
{
int a { 1 };
func(a == 1, &a);
}
Answer

func receives the result of evaluating the expression a == 1, as well as the address of a.

Changing a inside func does not change b, because b stores only the result, not the expression that produced it.