Asking Questions — Conditionals
"...but test everything; hold fast what is good." — 1 Thessalonians 5:21
Why This Matters
Programs that just compute and print have a ceiling. They can't decide. Every interesting program — every game, every utility, every tool you've ever used — is made interesting by the moments where it asks "is this true?" and goes one way or the other.
That's Pillar 2: Asking Questions. The mechanic is small. A handful of
operators (<, ==, &&). A new keyword
(if). But the consequence is huge: at the end of this chapter you can write
programs that respond, that branch, that handle bad input, that play games with the user.
The Shape of a Question
Every conditional in C++ has the same shape:
Compute a boolean. Then act on it.
if (age >= 18) {
cout << "You can vote." << endl;
}
The age >= 18 evaluates to true or false. The body
runs if true. Skips if false. That's the whole concept.
The = vs == Bug
This bug will catch you at least once. Possibly several times.
int score = 90;
if (score = 100) { // ⚠ single equals — BUG
cout << "Perfect!" << endl;
}
This compiles. It runs. It always prints "Perfect!" — because score = 100
assigns 100 to score AND evaluates to 100, which C++ treats as truthy. You changed your data
and triggered a false positive in one stroke.
Coach's Note —
=in C++ is not the equals sign from math class. It's a verb. The math-class equals sign is==. Wiring those two together in your head — one equals sign = "store this", two equals signs = "are these the same?" — saves you a year of confusion.
Combining Booleans: &&, ||, !
Real questions are rarely about one variable. They're about combinations.
A && B— true when both are true.A || B— true when at least one is true.!A— flip the boolean.
C++ uses short-circuit evaluation: in A && B, if A is
false, B is never evaluated. This lets you write things like
(divisor != 0) && (numerator / divisor > 10) — the division only runs
when divisor is safe.
Always Brace Your if Bodies
You can write a one-line if without braces, but here's the famous bug:
if (age >= 18)
cout << "Adult." << endl;
cout << "You can vote." << endl; // ⚠ always prints! The second line is NOT part of the if. The if controls only the very next statement. Indentation lied to your eyes. Always use braces.
This Week's Project
You're ready for Project 3: The Coffee Shop Conversation. You'll build a longer version of what you just played with — but in C++. Honest endings only. No "you converted your friend" outcomes. The conditionals you build this week power the whole thing.
Conditionals — Quick Check
int score = 90;
if (score = 100) {
cout << "Perfect!" << endl;
} else {
cout << "Not yet." << endl;
}
cout << "score is " << score << endl; = is assignment. score = 100 changes score to 100 and evaluates to 100, which is truthy — so the if body runs. The next line prints the modified score. Use == for comparison. A && B stops at A if A is false, so B is never evaluated. The first option checks length first, so name[0] only runs when the string is non-empty. The second option checks name[0] first — that crashes on empty strings. if (age >= 18)
cout << "Adult." << endl;
cout << "You can vote." << endl; cout is a separate statement that runs unconditionally. Always brace your if bodies, even one-liners.