C++ - Booleans
Overview
Estimated time: 20–30 minutes
Use bool for true/false values, build boolean expressions, and understand how integers convert to bool.
Learning Objectives
- Declare and evaluate boolean expressions.
- Understand implicit conversions between integers and bool.
Prerequisites
Examples
bool a = true; bool b = false;
bool c = (3 < 5); // true
bool d = (5 == 0); // false
bool e = 42; // true (non-zero)
bool f = 0; // false
Common Pitfalls
- Using assignment (=) instead of comparison (==) inside conditions.
- Relying on implementation-defined sizes of bool; use it as a logical type.