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 blocks of code
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 in C, and C has no separate true/false type — a condition is just a number, and any nonzero number counts as true.
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. Give it something to check inside parentheses, and a
brace-enclosed block underneath runs only when the answer is true. Follow that with else and a second
block, and you have covered the false case too. Exactly one of the two blocks runs. Never both, never
neither.
C has no separate true/false type sitting underneath this. A condition is just an integer expression, and
the rule is: zero is false, anything else is true. rain_chance >= umbrella_mark works out to 1 when
it holds and 0 when it does not, and if treats those exactly the way you would hope.
One equals sign or two
The question itself goes between the parentheses, and it is usually a comparison — two values held up against each other:
==asks whether two values are the same>=asks whether the left one is at least as big as the right one
That first one catches almost everybody once, because one equals sign and two equals signs are completely different instructions in C:
=stores.rain_chance = 70;puts 70 intorain_chance. It is a command, and it changes something.==compares.rain_chance == 70asks whether what is in there happens to be 70. It is a question, and on its own it changes nothing.
Unlike some languages, C will not stop you writing = inside an if — an assignment is a perfectly legal
expression, and its value is whatever was just stored. if (rain_chance = 0) compiles, assigns 0 and then
tests that 0, which is false, so the block quietly never runs; if (rain_chance = 5) compiles too, and
because 5 is nonzero the block runs on every single pass. Most compilers will warn about this. Read the
warning.
A worked example
A cinema till, not a weather report, so the answer to this exercise stays yours to write:
#include <stdio.h>
int main(void) {
int age = 14;
int adult_from = 18;
printf("Age ...... %d\n", age);
if (age >= adult_from) {
printf("Ticket type: adult\n");
} else {
printf("Ticket type: child\n");
}
printf("Enjoy the film.\n");
return 0;
}Running that prints three lines:
Age ...... 14
Ticket type: child
Enjoy the film.14 is not at least 18, so the first block is skipped over entirely and the second one runs. Only one ticket
line ever appears. Notice the last line too: it sits outside both braces, so it prints either way. Change
age to 30 and the middle line changes while the other two stay put.
Your turn
The editor holds a rain report. Two numbers sit at the top: rain_chance is today's forecast, and
umbrella_mark is the point at which carrying an umbrella is 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. That is because the test currently reads:
if (rain_chance == 0) {which asks whether the chance is exactly zero — a question with nothing to do with umbrellas, and one whose answer today is no.
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
The likeliest slip is the equals sign. if (rain_chance = umbrella_mark) compiles — it is legal C — and the
block then runs on every single pass, whatever the real forecast is, because it is testing the value it just
stored rather than comparing anything. If the advice never changes no matter what you set rain_chance to,
count the equals signs first.
Next likeliest is a stray semicolon straight after the closing parenthesis, if (...) ;. C reads that as an
if whose body is the empty statement that does nothing, and the printed advice below runs unconditionally,
looking exactly as if it belonged to the if.
Nothing here can break anything you cannot immediately fix. Read the diagnostic, find the line it names, and run it again.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
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)
- A single = is an assignment, not a question. C allows an assignment inside an if because an assignment is itself an expression with a value — this line would actually store umbrella_mark's value into rain_chance, and since that value is nonzero the block runs every single time, whatever the real forecast said. Most compilers warn about this; the warning is worth reading.
- Leaving the braces off and adding a second line to a branch
- Braces are optional in C when a branch is a single statement, but without them only the very next line belongs to the if — a second printf added below it would run unconditionally, every time, looking exactly as if it belonged to the block above it.
- 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 the mark to 80 and the test carries on comparing against the old 50, because that number was typed into the question instead of read from the variable.
- Forgetting the semicolon is not needed after the closing brace of if or else
- A stray semicolon straight after if (rain_chance >= umbrella_mark); creates an if whose body is an empty statement — the block below it then runs unconditionally every time, and the mistake produces no error at all, only advice that never changes with the forecast.
Longer explanation: read the full lesson. Want a blank editor instead? Open the C playground.