Wrong order vs right order
An if statement runs a block of code only when a boolean condition is true; else and else if add the other paths. Java checks the conditions top to bottom, runs the first branch that matches, and refuses any condition that isn't a real boolean.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a program that classifies a temperature t as freezing (below 0), cold (0–14), mild (15–24) or hot (25 and above). Test it with -5, 0, 20 and 30.
Given three ints a, b, c, print the largest using only if/else (no Math.max). Test with 3, 9, 4.
Print whether a number is positive, negative or zero, and on a second line whether it is even or odd. Test with -7 and 0.
Explain it without notes
What happens when two conditions in an else-if ladder are both true?
Explain the dangling else problem and how to avoid it.
Why does if (x = 5) fail to compile in Java when it compiles in C, and which variant of the typo still compiles?
What is definite assignment, and why does the compiler reject reading a variable assigned only inside an if?
Expected output
wrong order: D
right order: A