Chapter 03 · Week 3

Asking Questions — Conditionals

"...but test everything; hold fast what is good." — 1 Thessalonians 5:21
3

Walk a real conversation first

The widget below is a small version of what Project 3 will ask you to build in C++. Pick your responses. Watch the path light up. Notice that there are no "you won the debate" endings — that's intentional.

The Coffee Shop Conversation

Your friend asks the hardest question in the apologetic deck. Pick your replies — the path you take through the tree lights up, and an honest ending lands. No "you converted your friend" outcomes; this isn't that kind of program.

FRIEND WARMTH
0
friend shared pain
you acknowledged it
The same shape your code will take: nested ifs, boolean flags you set early and check later, combined conditions for the final ending dispatch. Project 3 asks you to build a longer version of this in C++.

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.

Check Your Reps

Conditionals — Quick Check

Question 1 of 4
What does this print?
int score = 90;
if (score = 100) {
    cout << "Perfect!" << endl;
} else {
    cout << "Not yet." << endl;
}
cout << "score is " << score << endl;
Why: Single = 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.
Question 2 of 4
Which expression safely checks that a string is non-empty before reading its first character?
Why: Short-circuit evaluation: 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.
Question 3 of 4
Why does this not behave as expected?
if (age >= 18)
    cout << "Adult." << endl;
    cout << "You can vote." << endl;
Why: An if without braces controls only the very next statement. The second cout is a separate statement that runs unconditionally. Always brace your if bodies, even one-liners.
Question 4 of 4
For the Coffee Shop project, which kind of ending is explicitly banned?
Why: The project spec is explicit: no "you converted your friend" endings. Real conversations about real suffering don't work that way, and a program that pretends they do is a bad witness. The other options are all legitimate honest endings.
YOU FINISHED. NICE WORK.

← WEEK 2: MEMORY   ·   WEEK 4: LOOPS →