Skip to content

Exercise 4 of 10 · Conditionals

Check the Temperature

What you will make

A short program that checks a temperature and prints whether it counts as a hot day.

The one new idea: if / else branching

Programs constantly need to make decisions based on data — if/else is how you tell R to run one block of code or another depending on a condition, which is the backbone of almost any interesting program.

Go straight to the code ↓

The concept

An if statement lets a program choose between two paths. You write if (condition) { ... } else { ... }, and R runs the first block when condition evaluates to TRUE, or the second block when it's FALSE. Conditions are usually built with comparison operators: >, <, >=, <=, == (equals), and != (not equals). In R, it's standard style to write the else keyword on the same line as the if block's closing brace — } else { — rather than starting a new line with else, which avoids a well-known parsing pitfall.

Worked example

r
stock <- 4

if (stock < 5) {
  cat("Reorder soon.\n")
} else {
  cat("Stock is fine.\n")
}
Output
Reorder soon.

Here, stock holds 4, and the condition stock < 5 checks whether that's true. Since 4 is indeed less than 5, the condition is TRUE, so only the if block's cat() call runs, printing "Reorder soon." The else block is skipped entirely — it only would have run if stock had been 5 or more.

Your turn

The starter code is meant to print "It's a hot day." whenever the temperature is above 30, but the condition currently compares against a different number entirely. Find that number in the if statement and change it to match the cutoff described in the comment just above it, so a temperature of 33 correctly triggers the hot-day message.

If something goes wrong

If the program prints "It's a mild day." when you expected the hot-day message, the condition is evaluating to FALSE when it should be TRUE — double-check the exact number and comparison operator being used, and mentally plug in temperature's actual value (33) to see which branch it should take. If you get a syntax error instead, make sure the else keyword is still on the same line as the closing } of the if block, and that both blocks' curly braces are properly matched.

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

Putting else on its own line after the closing brace of the if block, with nothing before it.
In R, when the if block's closing brace ends a complete statement on its own, a following else on a new line can cause a syntax error — the safe, standard style is to write `} else {` on the same line, exactly as the starter code already does.
Using = instead of == to compare values.
A single = is R's alternate assignment operator, not a comparison — comparing two values for equality requires the double equals sign, ==.
Editing the message strings instead of the comparison number.
The text "It's a hot day." and "It's a mild day." are already correct; the bug is which condition decides which message prints, not the wording of the messages themselves.

Want a blank editor instead? Open the R playground.