Skip to content

Exercise 4 of 10 · Conditions

Umbrella or Not

What you will make

A rain report card that reads the forecast for you and signs off with a straight answer — take the umbrella, or leave it at home.

The one new idea: if/else chooses between two different outcomes

Anything that reacts is a program choosing between outcomes: a low battery warning, a free delivery message, a seat belt chime. if and else are how that choice gets written down, and C++ is stricter than most languages about what counts as a real question.

Go straight to the code ↓

Asking a question before acting

Everything you have written so far ran straight down the page, every line, every time. That is fine for a banner. It is useless for advice, because advice depends on the situation.

if is how a program asks a question before it acts.

C++
if (condition) {
    // runs only when condition is true
} else {
    // runs only when condition is false
}

The parentheses hold the question. The braces hold the answer — one block for yes, one for no — and exactly one of the two ever runs. Braces are what group several lines into a single block; without them, only the one statement directly after if (...) belongs to it, which is worth remembering the first time you add a second line to a branch and forget to wrap both of them.

One equals sign or two — and why C++ will not save you

The comparison inside the parentheses is usually one of these:

  • == asks whether two values are equal
  • >= asks whether the left one is at least as big as the right one

Here is the part that catches C++ beginners harder than beginners in almost any other language. = and == are not two spellings of the same idea — they are two entirely different operators, and both of them are legal inside an if.

  • = assigns. rain_chance = 70; puts 70 into rain_chance and, as an expression, the assignment itself is worth the value that was just stored.
  • == compares. rain_chance == 70 asks whether what is already in there happens to be 70, and changes nothing.

Write if (rain_chance = umbrella_mark) and C++ compiles it without complaint. rain_chance is silently overwritten with whatever umbrella_mark holds, and the if then tests that new value: any number other than zero counts as true, so the branch runs almost every time regardless of the forecast, and a variable you never meant to touch has changed for the rest of the program. Many languages refuse to build a line like this. C++ does not, which is exactly why it is worth being able to spot on sight.

A worked example

A cinema till, not a weather report, so the answer to this exercise stays yours to write:

C++
#include <iostream>

int main() {
    int age = 14;
    int adult_from = 18;

    std::cout << "Age ...... " << age << std::endl;
    if (age >= adult_from) {
        std::cout << "Ticket type: adult" << std::endl;
    } else {
        std::cout << "Ticket type: child" << std::endl;
    }
    std::cout << "Enjoy the film." << std::endl;
}
Output
Age ...... 14
Ticket type: child
Enjoy the film.

14 is not at least 18, so the first block is skipped entirely and the second one runs. The last line sits outside both braces, so it prints either way. Change age to 30 and only the middle line moves.

Your turn

The editor holds a rain report. rain_chance is today's forecast, and umbrella_mark is the point at which carrying an umbrella becomes worth the bother.

Run it before changing anything. The card prints, and then tells you to leave the umbrella at home on a day with a seventy percent chance of rain — because the test currently reads:

C++
if (rain_chance == 0) {

which asks whether the chance is exactly zero, a question with nothing to do with umbrellas.

Replace that test with one asking whether rain_chance has reached umbrella_mark: at the mark or above it counts as take it. Everything else stays exactly as it is.

Then change rain_chance to 20 and run it again. The same program should now tell you to leave the umbrella at home. That second run is the real proof — it shows the program is reading the numbers rather than printing a fixed answer.

If something goes wrong

If the advice comes out backwards — take the umbrella on a dry day, every single time, no matter what the numbers say — count the equals signs in your test. A lone = compiles, assigns, and always seems to say yes.

If the message mentions braces or an "expected expression", the parentheses or one of the braces around a block have gone missing. Put them back exactly where they were.

Nothing here can break. Change it, run it, change it again.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Writing one equals sign in the test
if (rain_chance = umbrella_mark) compiles. That is the trap: one equals sign assigns umbrella_mark's value into rain_chance rather than comparing them, and the value an assignment leaves behind becomes the answer the if reads. Since umbrella_mark is 50, a nonzero number, the branch runs every single time, and rain_chance itself has quietly changed to 50 for the rest of the program. Python or JavaScript would have refused to build this line at all; C++ will not stop you.
Leaving the braces off the if or else block
C++ does not require braces around a single statement — only the one line right after if (...) belongs to it if you drop them. Add a second std::cout line meaning to keep it inside the branch and it silently escapes, running every time regardless of the forecast. The braces already in this exercise exist to keep both lines together on purpose.
Comparing the rain chance with a typed-in 50 instead of umbrella_mark
It gives the right advice today and the wrong advice later. Change umbrella_mark to 80 and a test hardcoded to 50 keeps using the old number, because that value was typed into the question instead of read from the variable.

Want a blank editor instead? Open the C++ playground.