Java if, if-else and the if-else-if Ladder: ICSE Class 8 with Outputs
Updated 17 October 2026 · Fact-checked
In Java, if runs a statement only when its condition is true, if-else chooses between two paths, and an if-else-if ladder chooses one of many by checking conditions from the top and running the first true one. The condition goes in brackets with no semicolon after it, and braces are needed when more than one statement belongs to the if or else.
Conditions are checked from the top; the first true one runs and the rest are skipped, so 82 fails m >= 90, passes m >= 75 and gets B.
Illustration drawn for QuestionBuddy.
Understand if, if-else and the if-else-if Ladder in Java
if. Runs a statement only when the condition is true:
if (n > 0)
System.out.println("Positive");if-else. Chooses between two paths:
if (n % 2 == 0)
System.out.println(n + " is even");
else
System.out.println(n + " is odd");For 27 the output is 27 is odd; for -4 it is -4 is even.
if-else-if ladder. Conditions are checked from the top; the first true one runs and the rest are skipped:
if (m >= 90)
g = 'A';
else if (m >= 75)
g = 'B';
else if (m >= 60)
g = 'C';
else
g = 'D';82 gives B; 90 gives A; 59 gives D.
Rules. The condition goes in brackets and must be true or false; there is no semicolon after if (condition); without braces only the first statement after if or else belongs to it, so use { } for more than one statement.
Braces trap. int m = 10; if (m > 5) m = m + 1; m = m * 2; prints 22, because only m = m + 1 belongs to the if and m = m * 2 always runs. Similarly with n = -4, if (n > 0) System.out.println("Positive"); System.out.println("Done"); prints only Done.
Linking with algorithms. The decision algorithms from the flowchart topics translate directly: each diamond becomes an if or else-if.
if, if-else and the if-else-if Ladder in Java: key facts
- Shapes of decision
if one path if-else two paths if-else-if ladder many paths - The first true condition in a ladder wins.
- Syntax rules
- Condition in brackets
- No semicolon after the condition
- Braces for several statements
- else has no condition
- A ; after if () makes the if do nothing.
- Grade ladder
m >= 90 A m >= 75 B m >= 60 C else D - 90 gives A; 60 gives C; 59 gives D.
How to answer if, if-else and the if-else-if Ladder in Java questions
Use this to trace or write decisions.
- 1Write the conditions from the strictest downwards in a ladder.
- 2Put each condition in brackets and leave out the semicolon.
- 3Use braces if an if or else has more than one statement.
- 4Trace a value through the conditions from the top and stop at the first true one.
- 5Test boundary values such as 90, 89, 75 and 60.
if, if-else and the if-else-if Ladder in Java in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
A hypothetical ticket price
Suppose a park charges 50 rupees for age under 12, 100 rupees for age 12 to 59, and 40 rupees for 60 and above (invented rates).
Write the ladder and give the price for ages 12, 11 and 60.
Try it first, then show the solutionSolution
- if (age < 12) price = 50;
- else if (age < 60) price = 100;
- else price = 40;
- Age 12: not < 12, is < 60: 100. Age 11: 50. Age 60: not < 60: 40.
Answer: The ages 12, 11 and 60 cost 100, 50 and 40 rupees.
Memory hook: Ladders choose the first true rung.
Tricks to remember if, if-else and the if-else-if Ladder in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
First true wins
In a ladder only the first true branch runs.
Use it when: Ladders.
No semicolon after if
if (x > 0); makes the if do nothing.
Use it when: Syntax.
if, if-else and the if-else-if Ladder in Java: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Which is correct? · 1 mark · MCQBoard level
Which if statement is written correctly?
(a) if (x > 0); (b) if x > 0 (c) if (x > 0) (d) if [x > 0]
Try it first, then show the solutionSolution
- The condition needs round brackets.
- There must be no semicolon.
Answer: (c) if (x > 0).
Example 2: Odd or even output · 2 marks · Short answerBoard level
Write the output of the if-else odd or even code for n = 27 and n = -4. (2 marks)
Try it first, then show the solutionSolution
- 27 % 2 is 1, so the else part runs: 27 is odd (1).
- -4 % 2 is 0: -4 is even (1).
Answer: 27 is odd and -4 is even.
Example 3: Trace the grade ladder · 4 marks · Long answerHarder board level
Give the grade g for m = 82, 90, 60 and 59. (4 marks)
Try it first, then show the solutionSolution
- 82 fails >= 90, meets >= 75: B (1).
- 90 meets >= 90: A (1).
- 60 fails >= 75, meets >= 60: C (1).
- 59 fails all: D (1).
Answer: B, A, C, D.
Example 4: The braces trap · 3 marks · Short answerHarder board level
Write the output:
int k = 4;
if (k > 2)
k = k + 3;
k = k * 2;
System.out.println(k);and explain. (3 marks)
Try it first, then show the solutionSolution
- 4 > 2 is true, so k = 7 (1).
- k = k * 2 always runs: 14 (1).
- Only the first statement belongs to the if (1).
Answer: Output 14. Only k = k + 3 belongs to the if; k = k * 2 is outside it and always runs.
Example 5: Correct the program · 4 marks · Long answerHOTS / competency
A student writes: if (marks >= 40); System.out.println("Pass"); else System.out.println("Fail"); Say what goes wrong and correct it. (4 marks)
Try it first, then show the solutionSolution
- The semicolon after the condition ends the if with an empty statement (1).
- The else then has no matching if and gives a compile error (1).
- Remove the semicolon (1).
- Correct: if (marks >= 40) System.out.println("Pass"); else System.out.println("Fail"); (1).
Answer: The semicolon after if (marks >= 40) ends the if, so the else has no if before it and the program does not compile. Remove it: if (marks >= 40) System.out.println("Pass"); else System.out.println("Fail");
Where students lose marks in if, if-else and the if-else-if Ladder in Java
The slips examiners see most often, and what to write instead.
Mistake: Semicolon after the condition
It ends the if immediately.
Wrong working
if (x > 0); print;
Right working
if (x > 0) print;
Do this: No semicolon after ).
Usually costs: 1 to 2 marks
Mistake: Putting two statements under an if without braces
Only the first belongs to the if.
Wrong working
if (m > 5) m = m + 1; m = m * 2; treated as both inside.
Right working
Use { } around both.
Do this: Use braces for more than one statement.
Usually costs: 1 mark
Mistake: Wrong order in a ladder
The first true condition wins.
Wrong working
Checking >= 60 before >= 90.
Right working
Check >= 90 first.
Do this: Strictest condition first.
Usually costs: 1 mark
Exam tips
- Trace from the top of the ladder and stop at the first true one.
- Always write the conditions with round brackets.
- Use braces when in doubt.
- Write the printed text exactly as in the code.
Practice questions on if, if-else and the if-else-if Ladder in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
In an if-else-if ladder, what happens after one condition is true?
(a) All remaining conditions are checked (b) The rest are skipped (c) The program stops (d) The first condition runs againHint
First true wins.
Answer
(b) The rest are skipped.
Q2 · 2 marks · Short answerBoard level
Write an if-else statement that prints Pass if marks >= 40 and Fail otherwise. (2 marks)
Hint
No semicolon after the condition.
Answer
if (marks >= 40) System.out.println("Pass"); else System.out.println("Fail");
Q3 · 3 marks · Short answerHarder board level
With n = -4, what does this print? if (n > 0) System.out.println("Positive"); System.out.println("Done"); Explain. (3 marks)
Hint
Which line belongs to the if?
Answer
It prints only Done. -4 > 0 is false so Positive is skipped, and the second println is outside the if and always runs.
Q4 · 4 marks · Long answerHarder board level
Write an if-else-if ladder for the sign of n printing Positive, Negative or Zero, and trace n = 0 and n = -7. (4 marks)
Hint
Two conditions.
Answer
if (n > 0) System.out.println("Positive"); else if (n < 0) System.out.println("Negative"); else System.out.println("Zero"); For n = 0 neither condition is true so Zero is printed; for -7, n < 0 is true so Negative is printed.
Q5 · 4 marks · Long answerHOTS / competency
A ladder is written: if (m >= 60) g = 'C'; else if (m >= 75) g = 'B'; else if (m >= 90) g = 'A'; else g = 'D'; Find g for 95 and explain the design fault. (4 marks)
Hint
The first condition is true for 95.
Answer
For 95, m >= 60 is true, so g = C and the rest are skipped. The ladder is in the wrong order: the strictest condition must come first (m >= 90, then 75, then 60), otherwise every mark of 60 or above gets C.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What does an if-else-if ladder do?Show answer
Chooses one of many paths by checking conditions from the top.
2.Is a semicolon allowed after if (condition)?Show answer
No.
3.What makes several statements part of an if?Show answer
Braces { }.
4.What does else need?Show answer
No condition; it runs when the if is false.
5.What is the grade for 90?Show answer
A.
6.What type must a condition be?Show answer
true or false.
if, if-else and the if-else-if Ladder in Java: frequently asked questions
Can an else exist without an if?
No.
Can I use a ladder instead of many separate ifs?
Yes, and it is clearer because only one branch runs.
Are brackets needed around the condition?
Yes, always.