bool
Topic: bool, flow control.
Video:
Concepts
Section titled “Concepts”- Boolean values (
true,false) - Boolean expressions
- Numeric comparison operators
>,<,>=,<=,==,!= - Boolean operators
&&,||,==,!,!= - Lazy evaluation of the operands of the
&&and||operators - Implicit conversion to
boolfromintand pointers - The
NULLmacro - The special
nullptrvalue
Examples to aid understanding
Section titled “Examples to aid understanding”1. true
Section titled “1. true”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.
2. 0 and 1 as bool
Section titled “2. 0 and 1 as bool”bool a { 0 };bool b { 1 };Answer
You can also store 0 instead of false, and 1 instead of true.
This compiles successfully.
3. Converting int to bool
Section titled “3. Converting int to bool”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.
4. Size of bool
Section titled “4. Size of bool”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.
5. bool expression (1)
Section titled “5. bool expression (1)”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.
6. bool expression (2)
Section titled “6. bool expression (2)”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.
7. bool expression (3)
Section titled “7. bool expression (3)”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.
8. bool expression (4)
Section titled “8. bool expression (4)”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.
9. Reassigning a bool variable
Section titled “9. Reassigning a bool variable”bool a = 1 > 2;a = 3 == 3;std::cout << a << std::endl;Answer
Here, a is reassigned from false to another value (true).
10. Passing a bool variable to a function
Section titled “10. Passing a bool variable to a function”#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.
13. Returning a bool value
Section titled “13. Returning a bool value”#include <iostream>
bool f(){ return true;}
int main(){ bool result = f(); std::cout << result << std::endl;}14. Example of a Boolean function
Section titled “14. Example of a Boolean function”#include <iostream>
bool IsGreater(int a, int b){ return a > b;}
int main(){ bool result = IsGreater(5, 6); std::cout << result << std::endl;}15. Comparing Boolean variables
Section titled “15. Comparing Boolean variables”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.
16. The ! operator
Section titled “16. The ! operator”bool a = false;bool b = !a;std::cout << b << std::endl;Answer
The ! operator turns false into true (and vice versa).
17. The && operator
Section titled “17. The && operator”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.
18. Functions that are never called
Section titled “18. Functions that are never called”#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.
19. Nuances of the && operator (1)
Section titled “19. Nuances of the && operator (1)”#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.
AB20. Nuances of the && operator (2)
Section titled “20. Nuances of the && operator (2)”#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.
AB21. Nuances of the && operator (3)
Section titled “21. Nuances of the && operator (3)”#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.
A22. Nuances of the || operator (4)
Section titled “22. Nuances of the || operator (4)”#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.
A23. 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.