C++ Tutorial
C++ Booleans
bool is true or false. Comparisons produce booleans. Conditions consume them.
true or false
A bool stores one of two values: true or false. That is the whole type. You use it to remember a yes-or-no fact: a door is open, a score is high enough, a loop should keep going.
Write the literals in lowercase. True and False are Python. C++ will not compile them.
A complete program
Declare a bool, print it, then print a comparison. By default cout prints booleans as1 and 0, not the words true and false.
Example
#include <iostream>
using namespace std;
int main() {
bool ready = true;
bool empty = false;
cout << ready << endl;
cout << empty << endl;
cout << (7 > 3) << endl;
cout << (7 == 3) << endl;
return 0;
}Run this in /cpp/try. You should see 1, 0, 1,0.
Comparisons produce booleans
Every comparison expression has type bool. You can store it, print it, or pass it straight intoif. The operators are the same ones you saw in Operators.
| Expression | True when |
|---|---|
a == b | a equals b |
a != b | a is not equal to b |
a < b | a is less than b |
a <= b | a is less than or equal to b |
a > b | a is greater than b |
a >= b | a is greater than or equal to b |
Use == to compare. A single = assigns. Writing if (x = 1) setsx to 1 and then treats that as true. The compiler may warn; the program is still wrong.
Combine with and, or, not
C++ uses && (and), || (or), and ! (not). &&is true only if both sides are true. || is true if at least one side is true. ! flips a value.
Example
#include <iostream>
using namespace std;
int main() {
int age = 20;
bool member = true;
cout << (age >= 18 && member) << endl;
cout << (age < 18 || member) << endl;
cout << (!member) << endl;
return 0;
}Print the words true and false
Insert boolalpha into the stream if you want text instead of 1 and 0. It stays on until you switch back with noboolalpha.
Example
#include <iostream>
using namespace std;
int main() {
bool ok = true;
cout << boolalpha;
cout << ok << endl;
cout << (2 == 3) << endl;
return 0;
}Conditions consume them
An if, a while, and the middle part of a for all expect something that converts to bool. You will write that in the next chapters. The value itself is still just true or false.
Integers convert too: 0 is false, any other number is true. That is why a forgotten== can look like a working condition. Prefer an actual bool or a comparison.
What to remember
boolholdstrueorfalse.- Comparisons such as
==and<produce a bool. coutprints 1 and 0 unless you useboolalpha.- Combine tests with
&&,||, and!.
Next: if and else — the first place those booleans get used.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Acidic or not
On the pH scale, 7 is neutral at 25 °C. Below 7 is acidic, above 7 is alkaline. 4.2 is lemon-juice territory, not a strong mineral acid, but it is still acidic.
bool stores that yes/no. boolalpha prints true and false as words instead of 1 and 0.
acidic ⇔ pH < 7
pH 7 is exactly neutral only at 25 °C. Kw changes with temperature, so the neutral point drifts a little.
Example
#include <iostream>
using namespace std;
int main() {
double ph = 4.2;
bool acidic = ph < 7.0;
cout << boolalpha << acidic << endl;
return 0;
}Physics
Ice or liquid water
At standard pressure, ice melts at 0 °C. A freezer at −3 °C keeps water solid. The boolean is the whole scientific question for this check: is the sample above that point?
liquid if t > 0 °C (at 1 atm)
Example
#include <iostream>
using namespace std;
int main() {
double celsius = -3.0;
bool liquid_water = celsius > 0.0;
cout << (liquid_water ? "yes" : "no — ice") << endl;
return 0;
}