Java Conditional Statements: if, switch and ?: (ISC Class 11 Computer Science)
Updated 17 October 2026 · Fact-checked
A statement ends with a semicolon, and a block groups statements in braces. The if statement runs code when a boolean condition is true; if-else chooses between two blocks and if-else-if between many. The switch statement selects a case by an int, char or String value; without break, execution falls through to the following cases. The ternary operator condition ? a : b picks one of two values. In Java a condition must be a boolean expression.
With d = 3, execution jumps to case 3 and keeps going into case 4 until break stops it, so the output is Wed Thu.
Illustration drawn for QuestionBuddy.
Understand Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator
Statements and blocks. A simple statement ends with ;. A block { ... } groups statements and can be used wherever one statement is allowed; variables declared inside a block exist only within it.
if, if-else, if-else-if.
if (m >= 90) grade = 'A';
else if (m >= 70) grade = 'B';
else if (m >= 50) grade = 'C';
else grade = 'D';Conditions are tested from the top; the first true one runs and the rest are skipped. The condition must be boolean: if (x = 5) is a compile-time error (an int is not a boolean); write x == 5.
Dangling else. An else joins the nearest unmatched if: in if (a > 5) if (b > 5) System.out.print("X"); else System.out.print("Y"); the else belongs to the inner if, so for a = 3 nothing is printed.
switch.
switch (d) {
case 1: System.out.print("Mon ");
case 2: System.out.print("Tue "); break;
case 3: System.out.print("Wed ");
case 4: System.out.print("Thu "); break;
default: System.out.print("Off");
}The expression may be int, char, byte, short or String. Execution jumps to the matching case and continues until a break (*fall-through*); default runs when no case matches. For d = 3 the output is Wed Thu ; for d = 2: Tue ; for d = 5: Off.
Ternary operator. condition ? valueIfTrue : valueIfFalse. int max = (a > b) ? a : b; gives 9 for a = 7, b = 9.
Logical operators in conditions. && (and), || (or), ! (not); they short-circuit: in a != 0 && b / a > 2 the division is not evaluated when a is 0.
Leap year. (y % 4 == 0 && y % 100 != 0) || y % 400 == 0: 2024 true, 2100 false, 2000 true.
Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator: key facts
- Ternary
- result = condition ? valueIfTrue : valueIfFalse | Both values must suit the type
- Replaces a simple if-else.
- Leap year
- (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 | 2000 true, 2100 false
- Gregorian rule.
- switch flow
- Jump to matching case
- Continue until break
- default if no match
- Fall-through.
How to answer Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator questions
Evaluate the condition with the given values and follow exactly one path.
- 1Substitute the values into the condition.
- 2Take the first true branch in an if-else-if chain.
- 3For switch, jump to the matching case and continue until a break.
- 4Write the output exactly, with spaces.
Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator 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.
Fall-through
Suppose a switch prints a day name for each case but the programmer forgets break after Monday.
What happens on Monday?
Try it first, then show the solutionSolution
- Control enters case 1 and, with no break, continues into case 2.
- Both Mon and Tue are printed.
Answer: Both Monday and Tuesday are printed.
Memory hook: No break, no stop.
Tricks to remember Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator
Short memory aids. Say each one aloud once, then use it on a question the same day.
First true wins
Else-if stops at the first true condition.
Use it when: if-else-if.
Break to stop
Without break, switch falls through.
Use it when: switch.
Equals twice
Compare with ==, assign with =.
Use it when: Conditions.
Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Condition · 1 mark · MCQBoard level
Which statement causes a compile-time error in Java?
(a) if (x == 5) (b) if (x = 5) (c) if (x > 5 && x < 9) (d) if (!(x < 5))
Try it first, then show the solutionSolution
- x = 5 is an assignment giving an int, not a boolean.
Answer: (b) if (x = 5)
Example 2: switch output · 4 marks · Short answerBoard level
Give the output of the switch statement above for d = 1, 2, 3 and 5. (4 marks)
Try it first, then show the solutionSolution
- d = 1: case 1 prints Mon, falls to case 2 prints Tue, break: "Mon Tue ".
- d = 2: "Tue ".
- d = 3: Wed, falls to Thu, break: "Wed Thu ".
- d = 5: no case, default: "Off".
Answer: Mon Tue ; Tue ; Wed Thu ; Off
Example 3: Ternary and nested ternary · 5 marks · Short answerHarder board level
Find the output: int a = 7, b = 9; int max = (a > b) ? a : b; String s; int x = 15; s = x % 15 == 0 ? "FizzBuzz" : x % 5 == 0 ? "Buzz" : x % 3 == 0 ? "Fizz" : "" + x; System.out.println(max + " " + s); Then give s for x = 10, 9 and 7. (5 marks)
Try it first, then show the solutionSolution
- max = 9.
- x = 15: 15 % 15 == 0, so FizzBuzz. Output: 9 FizzBuzz.
- x = 10: not divisible by 15, 10 % 5 == 0: Buzz.
- x = 9: Fizz. x = 7: "7".
Answer: 9 FizzBuzz; for x = 10: Buzz; x = 9: Fizz; x = 7: 7
Example 4: Program: leap year and grade · 4 marks · Short answerHarder board level
Write a program fragment to test whether a year is a leap year using one condition and give the result for 2024, 2100, 2000 and 1900. (4 marks)
Try it first, then show the solutionSolution
- Condition: (y % 4 == 0 && y % 100 != 0) || y % 400 == 0.
- 2024: divisible by 4, not by 100: true.
- 2100: divisible by 100 but not 400: false.
- 2000: divisible by 400: true. 1900: false.
Answer: if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) System.out.println("Leap"); else System.out.println("Not leap"); Results: 2024 Leap; 2100 Not leap; 2000 Leap; 1900 Not leap.
Example 5: Dangling else · 5 marks · Long answerHOTS / competency
Give the output for (a = 3, b = 10), (a = 8, b = 3) and (a = 8, b = 10): if (a > 5) if (b > 5) System.out.print("X"); else System.out.print("Y"); Add braces so that the else belongs to the outer if and state the new outputs. (5 marks)
Try it first, then show the solutionSolution
- The else belongs to the inner if.
- (3, 10): outer false, nothing printed.
- (8, 3): outer true, inner false: Y.
- (8, 10): X.
- With braces: if (a > 5) { if (b > 5) print X; } else print Y; the outputs become (3, 10): Y; (8, 3): nothing; (8, 10): X.
Answer: (3,10): nothing; (8,3): Y; (8,10): X. With braces for the outer if: Y, nothing, X.
Where students lose marks in Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator
The slips examiners see most often, and what to write instead.
Mistake: Using = in a condition
Typing slip.
Wrong working
if (x = 5)
Right working
if (x == 5)
Do this: == compares.
Usually costs: 1-2 marks.
Mistake: Forgetting break in switch
Assuming it stops.
Wrong working
Only Wed printed for 3
Right working
Wed Thu
Do this: Check for break.
Usually costs: 2 marks.
Mistake: Pairing else with the outer if
Looks aligned.
Wrong working
Y for (3, 10)
Right working
Nothing
Do this: Else joins the nearest if.
Usually costs: 2 marks.
Exam tips
- Trace with the given values written next to the condition.
- Mark where each break is.
- Use braces to avoid dangling else.
Practice questions on Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator
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
The output of int x = 5; String r = (x > 3) ? "yes" : "no"; is:
(a) yes (b) no (c) true (d) errorHint
5 > 3.
Answer
(a) yes
Q2 · 2 marks · Short answerBoard level
Write the output for k = 3 and then for k = 1: switch (k) { case 1: System.out.print("A"); case 2: System.out.print("B"); break; case 3: System.out.print("C"); case 4: System.out.print("D"); default: System.out.print("E"); } (2 marks)
Hint
Look for the breaks.
Answer
k = 3: CDE (falls through case 4 into default). k = 1: AB (stops at the break).
Q3 · 4 marks · Short answerHarder board level
Write a Java fragment to print "Positive", "Negative" or "Zero" for an integer n, using if-else-if, and a one-line ternary version for "Even" or "Odd". Give the outputs for n = -4 and n = 7. (4 marks)
Hint
n % 2 == 0.
Answer
if (n > 0) ... else if (n < 0) ... else ... ; String t = (n % 2 == 0) ? "Even" : "Odd"; For n = -4: Negative, Even. For n = 7: Positive, Odd.
Q4 · 5 marks · Long answerHarder board level
A shop gives a discount: 20% on bills above Rs 5,000, 10% on bills above Rs 2,000 up to Rs 5,000, and none otherwise. Write the if-else-if ladder and the payable amount for bills of Rs 6,000, Rs 2,500 and Rs 1,800. (5 marks)
Hint
Test the largest bill first.
Answer
if (b > 5000) pay = b * 0.80; else if (b > 2000) pay = b * 0.90; else pay = b; 6,000 gives 4,800; 2,500 gives 2,250; 1,800 gives 1,800.
Q5 · 3 marks · Short answerHOTS / competency
A student writes: int a = 0; if (a != 0 & 10 / a > 2) ... and another writes if (a != 0 && 10 / a > 2) ... Explain which one fails when a is 0 and why. (3 marks)
Hint
Short-circuit.
Answer
The first uses the non-short-circuit & so 10 / a is evaluated and throws ArithmeticException; the second uses && and stops at a != 0 being false, so the division is never evaluated.
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.Condition type in if?Show answer
boolean.
2.switch falls through without?Show answer
break.
3.Ternary form?Show answer
condition ? a : b.
4.Else belongs to?Show answer
The nearest unmatched if.
5.&& is?Show answer
Short-circuit AND.
Conditional Statements in Java: if, if-else-if, switch and the Ternary Operator: frequently asked questions
Can switch use a String?
Yes (Java 7 onwards).
Is default compulsory?
No.
Can if be nested?
Yes.
What is a block?
Statements in braces.