if, if-else, if-else-if Ladder and Nested if in Java with Output Tracing (ICSE Class 9)
Updated 18 October 2026 · Fact-checked
A conditional statement chooses which statements run. if runs a block only when its condition is true; if-else chooses one of two blocks; an if-else-if ladder tests conditions one after another and runs the first true block; a nested if places one if inside another. The condition must be boolean and written in brackets, and an else belongs to the nearest unmatched if.
Tests run from the top; the first true condition (marks >= 75) runs its block, then the rest of the ladder is skipped.
Illustration drawn for QuestionBuddy.
Understand if, if-else, if-else-if Ladder and Nested if with Output Tracing
if. if (condition) statement; The statement (or the block in braces) runs only when the condition is true. Otherwise control passes to the next statement.
if-else. if (condition) block1; else block2; Exactly one block runs.
Braces. Without braces the if controls only the next single statement. Use braces whenever more than one statement belongs to the if or else.
if-else-if ladder. if (c1) ... else if (c2) ... else if (c3) ... else ... The conditions are tested from the top. The first true condition's block runs and the rest of the ladder is skipped. The final else (optional) handles everything else. Write the conditions so that the strictest comes first (for marks: 90 or more, then 75 or more, and so on); the lower bound of each range is already guaranteed by the failed tests above it.
Nested if. An if (or else) block that contains another if. The inner if is tested only when the outer condition allows it.
Dangling else. In if (a) if (b) X; else Y; the else belongs to the nearest unmatched if, that is if (b). Braces can change this.
Conditions. Use relational operators (== != < > <= >=) and logical operators (&& || !). Use == to compare, never a single =. A condition such as marks >= 40 && marks <= 100 joins two comparisons.
Frequent slips. A semicolon straight after if (condition) ends the statement, so the next line always runs. Comparing with = instead of == gives a compile error in Java for int values because the condition would not be boolean.
Typical programs. Grade from marks, bill by slab of units, type of triangle, positive or negative number, eligibility by age.
Neighbouring topics: switch-case and menu-driven programs are on the next page of this chapter.
if, if-else, if-else-if Ladder and Nested if with Output Tracing: key facts
- if-else
- if (condition) block1
- else block2
- one block runs
- Condition in brackets, boolean.
- Ladder rule
- Conditions tested from the top
- first true block runs
- rest skipped
- final else for the remainder
- Order the conditions carefully.
- Dangling else
- else joins the nearest unmatched if | braces change the pairing
- Check the indentation against the pairing.
- Equality
- == compares
- = assigns
- if (x = 5) is an error for int x
- Use == in conditions.
How to answer if, if-else, if-else-if Ladder and Nested if with Output Tracing questions
Use this routine to trace a conditional program.
- 1Write the value of each variable.
- 2Test the first condition: if true, run its block and skip the rest of the ladder.
- 3If false, move to the next else-if or to the final else.
- 4For nested if statements, test the inner condition only when the outer one is true.
- 5After the structure ends, continue with the next statement and write the output.
if, if-else, if-else-if Ladder and Nested if with Output Tracing 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.
Fare by age
Suppose a museum charges nothing below 5 years, Rs 50 from 5 to 12, Rs 100 from 13 to 59 and Rs 60 from 60 onwards.
What does a visitor aged 12 pay and what does one aged 60 pay? Why does the ladder not need a lower bound (age >= 13) for the 13 to 59 band?
Try it first, then show the solutionSolution
- Age 12 is not below 5 but is 12 or less: Rs 50.
- Age 60 fails every test up to 59 and reaches the last case: Rs 60.
- Because the tests above have already failed, the lower bound is known.
Answer: Age 12 pays Rs 50 and age 60 pays Rs 60.
Memory hook: The first true test wins.
Tricks to remember if, if-else, if-else-if Ladder and Nested if with Output Tracing
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 block runs.
Use it when: Ladder tracing.
Else sticks to the nearest if
The dangling else binds to the closest unmatched if.
Use it when: Nested if tracing.
No semicolon after the if bracket
if (x > 0); is an empty statement.
Use it when: Spotting errors.
if, if-else, if-else-if Ladder and Nested if with Output Tracing: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Skipped statement · 1 mark · MCQBoard level
What is printed?
int n = 7;
if (n % 2 == 0)
System.out.println("Even");
System.out.println("Done");(a) Even (b) Done (c) Even and Done (d) nothing
Try it first, then show the solutionSolution
- 7 % 2 = 1, so the condition is false and the first println is skipped.
- The second println is outside the if and runs.
Answer: (b) Done
Example 2: Ladder at the boundaries · 3 marks · Short answerHarder board level
The grade of a student is found by:
char g;
if (m >= 90) g = 'A';
else if (m >= 75) g = 'B';
else if (m >= 60) g = 'C';
else if (m >= 40) g = 'D';
else g = 'F';Find g for m = 75, m = 59 and m = 90.
Try it first, then show the solutionSolution
- m = 75: 75 >= 90 is false, 75 >= 75 is true, so g = B. (1 mark)
- m = 59: fails 90, 75 and 60; 59 >= 40 is true, so g = D. (1 mark)
- m = 90: the first condition is true, so g = A and the rest is skipped. (1 mark)
Answer: B, D, A
Example 3: Nested if and the dangling else · 3 marks · Short answerHarder board level
Find the output for a = 10 and again for a = 3:
int b = 5, c = 8;
if (a > b)
if (b > c)
System.out.println("X");
else
System.out.println("Y");
System.out.println("Z");Try it first, then show the solutionSolution
- The else pairs with the nearest unmatched if, which is if (b > c). (1 mark)
- a = 10: a > b is true; b > c (5 > 8) is false, so the else prints Y; then Z is printed. Output: Y and Z on two lines. (1 mark)
- a = 3: a > b is false, so the whole inner if-else is skipped; only Z is printed. (1 mark)
Answer: For a = 10: Y then Z. For a = 3: Z.
Example 4: Case study: electricity bill · 4 marks · Case-basedHOTS / competency
A bill is calculated by:
double bill;
if (units <= 100)
bill = units * 2;
else if (units <= 200)
bill = 200 + (units - 100) * 3;
else
bill = 200 + 300 + (units - 200) * 5;
System.out.println(bill);(i) Find the bill for 150 units. (1)
(ii) Find the bill for 250 units. (2)
(iii) Explain the constants 200 and 300. (1)
Try it first, then show the solutionSolution
- (i) 150 <= 200, so bill = 200 + 50 x 3 = 350.0.
- (ii) 250 > 200, so bill = 200 + 300 + 50 x 5 = 500 + 250 = 750.0.
- (iii) 200 is the charge for the first 100 units (100 x 2) and 300 is the charge for the next 100 units (100 x 3).
Answer: (i) 350.0 (ii) 750.0 (iii) 200 = first 100 units at Rs 2; 300 = next 100 units at Rs 3
Example 5: Type of triangle · 3 marks · Short answerHarder board level
Find the output for the sides (7, 7, 4) and (6, 6, 6):
if (a == b && b == c)
System.out.println("Equilateral");
else if (a == b || b == c || a == c)
System.out.println("Isosceles");
else
System.out.println("Scalene");Try it first, then show the solutionSolution
- (7, 7, 4): a == b is true but b == c is false, so the first condition is false. (1 mark)
- a == b is true, so the second condition is true: Isosceles. (1 mark)
- (6, 6, 6): all three are equal, so the first condition is true: Equilateral (the ladder stops there). (1 mark)
Answer: Isosceles for (7, 7, 4); Equilateral for (6, 6, 6)
Where students lose marks in if, if-else, if-else-if Ladder and Nested if with Output Tracing
The slips examiners see most often, and what to write instead.
Mistake: Semicolon after the condition
The if line is treated like a statement.
Wrong working
if (n > 0); then println always runs
Right working
if (n > 0) println(...)
Do this: No semicolon after the bracket.
Usually costs: 1 to 2 marks
Mistake: Single = in a condition
Assignment and comparison are confused.
Wrong working
if (x = 5)
Right working
if (x == 5)
Do this: Use == to compare.
Usually costs: 1 mark
Mistake: Wrong order in a ladder
The broadest test is placed first.
Wrong working
if (m >= 40) before if (m >= 90)
Right working
test m >= 90 first
Do this: Strictest condition first.
Usually costs: 2 marks
Exam tips
- Always use braces for more than one statement.
- In a ladder, say "the rest is skipped" once a condition is true.
- Check boundary values (75, 40, 90) when you trace.
- Indent your code to show which else belongs to which if.
Practice questions on if, if-else, if-else-if Ladder and Nested if with Output Tracing
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
Which is the correct form?
(a) if (x = 5) (b) if (x == 5) (c) if x == 5 (d) if (x == 5);Hint
The condition is in brackets and has no semicolon.
Answer
(b) if (x == 5)
Q2 · 2 marks · Very short answerBoard level
Write an if-else statement to print "Pass" if marks are at least 40, otherwise "Fail".
Hint
Use >=.
Answer
if (marks >= 40)
System.out.println("Pass");
else
System.out.println("Fail");Q3 · 3 marks · Short answerHarder board level
Find the output:
int x = 15; if (x > 10) { x = x - 5; if (x % 2 == 0) x = x / 2; else x = x * 3; } else x = 0; System.out.println(x);Hint
Work through the outer block first.
Answer
5 (x > 10, so x = 10; 10 % 2 == 0, so x = 10 / 2 = 5)
Q4 · 3 marks · Short answerHarder board level
For n = -4 the code below prints Positive. Explain why and correct it.
if (n > 0); System.out.println("Positive");Hint
What does the semicolon do?
Answer
The semicolon after the bracket is an empty statement that belongs to the if, so the println is outside it and always runs. Remove the semicolon: if (n > 0) System.out.println("Positive");
Q5 · 4 marks · Short answerHOTS / competency
A ticket costs Rs 0 below age 5, Rs 50 from 5 to 12, Rs 100 from 13 to 59 and Rs 60 from 60 onwards. Write an if-else-if ladder that stores the price in an int variable fare, and give fare for the ages 4, 12, 13 and 60.
Hint
Test the smallest range first.
Answer
if (age < 5) fare = 0;
else if (age <= 12) fare = 50;
else if (age <= 59) fare = 100;
else fare = 60;
Fares: age 4 gives 0; age 12 gives 50; age 13 gives 100; age 60 gives 60.
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.How many blocks run in an if-else?Show answer
Exactly one.
2.What does a final else do in a ladder?Show answer
It handles every case not matched above.
3.The else belongs to which if?Show answer
The nearest unmatched if.
4.Which operator compares two values?Show answer
==
5.What does if (x > 0); do?Show answer
The empty statement is the body of the if.
6.What type must a condition have?Show answer
boolean
if, if-else, if-else-if Ladder and Nested if with Output Tracing: frequently asked questions
Can an if have no else?
Yes. The else part is optional.
Do I need braces for a single statement?
No, but braces are safer and are expected in long answers.
Can conditions overlap in a ladder?
Yes, but only the first true block runs, so order matters.