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 outcomes, and the test inside it must itself be true or false
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 Java insisting the question itself must be true or false is a rule that catches a real class of mistakes before they ever run.
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. You give it something to check, and inside a pair of
curly braces a block of lines that run only when the answer is yes. Follow that with else and a second
braced block, and you have covered the no as well. Exactly one of the two blocks runs. Never both, never
neither.
if (rainChance >= umbrellaMark) {
System.out.println("Rain is likely today.");
} else {
System.out.println("It should stay dry.");
}The braces are what mark off each block, the same way they marked off the body of main itself. Indentation
helps you read it; the braces are what Java actually reads.
The question has to be a boolean
Java is strict about one thing here that trips almost everybody once: whatever sits inside the round brackets
of an if has to be a boolean — a value that is either true or false, nothing else. A comparison
produces one of those automatically:
==asks whether two values are the same, and hands backtrueorfalse>=asks whether the left one is at least as big as the right one, and does the same
A single equals sign does something completely different. rainChance = umbrellaMark is not a question at
all — it stores umbrellaMark's value into rainChance and, as an expression, that whole thing works out
to an int. Put that inside an if, and javac refuses it outright: an int is not a boolean, and no
amount of running the program will make it one. This is not a warning either. incompatible types: int cannot be converted to boolean stops compilation before a single line executes, which means the report above the
if never gets the chance to print. Once you know to look for it, the fix is always the same: a second equals
sign, or whichever comparison you actually meant.
A worked example
A cinema till, not a weather report, so the answer to this exercise stays yours to write:
public class Main {
public static void main(String[] args) {
int age = 14;
int adultFrom = 18;
System.out.println("Age ...... " + age);
if (age >= adultFrom) {
System.out.println("Ticket type: adult");
} else {
System.out.println("Ticket type: child");
}
System.out.println("Enjoy the film.");
}
}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. The last line sits outside both braces, so it runs 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: rainChance is today's forecast, and
umbrellaMark 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 (rainChance == 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 rainChance has reached umbrellaMark: at the mark or above it
counts as take it. Everything else stays exactly as it is.
Then change rainChance 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. Written as if (rainChance = umbrellaMark), javac stops on the spot
with incompatible types: int cannot be converted to boolean, and nothing runs, not even the card above it.
That message is Java refusing to treat a stored value as a yes-or-no answer. Add the second equals sign, or
the comparison you actually meant.
If the message instead mentions a missing brace or bracket, count them against the worked example above —
every if and else here needs both an opening and a closing { } pair. Nothing here can break.
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 a single equals sign in the test
- if (rainChance = umbrellaMark) tries to store umbrellaMark's value into rainChance rather than ask a question, and the result of doing that is an int, not a true-or-false answer. Java requires the inside of an if to be a boolean, so javac refuses the whole program before any of it runs, reporting incompatible types: int cannot be converted to boolean — not even the report above the if prints.
- Comparing the rain chance with a typed-in 50 instead of umbrellaMark
- if (rainChance >= 50) gives the right advice today and the wrong advice later. Change umbrellaMark to 80 and the test carries on using the old 50, because that number was typed straight into the question instead of read from the variable meant to hold it.
- Leaving the braces off one branch
- Java lets a single statement follow if or else without braces, but every branch here already holds two println lines, and only the first would run without them — the second would sit outside the branch entirely and always execute, whichever way the forecast goes. Keeping the braces around both lines is what keeps them together.
- Reversing the operator to <=
- The advice flips itself: a seventy percent chance against a fifty percent mark would now say to leave the umbrella at home, since seventy is not at or below fifty. The test needs at least as big as, not at most.
Want a blank editor instead? Open the Java playground.