Conditional Statements in Java: ICSE Class 9 Notes on if, if-else and switch
ICSE Class 9 Computer Applications notes on conditional statements in Java: if, if-else, if-else-if ladder, nested if, the ternary operator and switch-case with fall-through, with solved programs and output questions.

A program often has to make decisions: is the number even, has the student passed, which slab does the electricity bill fall in? Conditional statements let a Java program choose what to do based on a condition. This chapter is the base of almost every program you will write in ICSE Class 9 and 10.
1. The if statement
The if statement runs a block only when the condition is true.
if (marks >= 40) {
System.out.println("Pass");
}2. The if-else statement
The if-else statement chooses between two paths.
if (n % 2 == 0)
System.out.println(n + " is even");
else
System.out.println(n + " is odd");3. The if-else-if ladder
Use an if-else-if ladder for several conditions checked in order. The first true condition wins, and the rest are skipped.
if (percent >= 90)
grade = 'A';
else if (percent >= 75)
grade = 'B';
else if (percent >= 60)
grade = 'C';
else
grade = 'D';The order matters. If you test percent >= 60 first, a student with 95 would get grade C.4. Nested if
A nested if is an if placed inside another if. Use it when one decision depends on another.
if (a > b) {
if (a > c)
System.out.println("Largest: " + a);
else
System.out.println("Largest: " + c);
} else {
if (b > c)
System.out.println("Largest: " + b);
else
System.out.println("Largest: " + c);
}5. The ternary operator
The ternary operator is a one-line if-else that returns a value:
int max = (a > b) ? a : b;
String result = (marks >= 40) ? "Pass" : "Fail";6. The switch statement
switch compares one variable against several fixed values (cases). It works with int, char, String and some other types, but not with double, float, long or boolean.
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}breakends the switch. Without break, execution "falls through" into the next case.defaultruns when no case matches. It is optional.
Fall-through: an output question
int x = 2;
switch (x) {
case 1: System.out.print("One ");
case 2: System.out.print("Two ");
case 3: System.out.print("Three ");
default: System.out.print("Other");
}Output: Two Three Other. There are no break statements, so once case 2 matches, every following statement runs.
if-else vs switch
- What it tests: if-else can test any boolean condition, including ranges like
x > 10; switch tests one variable against exact values. - Data types: if-else works with every type; switch does not work with double, float, long or boolean.
- Best for: if-else suits ranges and complex conditions; switch suits menus and fixed choices.
Solved program: menu-driven calculator
import java.util.Scanner;
class Calculator {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
double a = sc.nextDouble(), b = sc.nextDouble();
System.out.print("Enter operator (+ - * /): ");
char op = sc.next().charAt(0);
switch (op) {
case '+': System.out.println("Result = " + (a + b)); break;
case '-': System.out.println("Result = " + (a - b)); break;
case '*': System.out.println("Result = " + (a * b)); break;
case '/':
if (b != 0)
System.out.println("Result = " + (a / b));
else
System.out.println("Division by zero is not allowed");
break;
default:
System.out.println("Invalid operator");
}
}
}The program combines a switch (to choose the operation) with an if-else inside one case (to prevent division by zero).
Practice questions
- Write a program to check whether a year is a leap year. *Hint: divisible by 4 and not by 100, or divisible by 400.*
- Give the output:
int a = 5; if (a > 3) if (a > 10) System.out.print("X"); else System.out.print("Y"); - Why can't
switchbe used to check whethermarks > 40?
Answers:
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)means it is a leap year.Y. Theelsebelongs to the nearestif, which isif (a > 10).- Because switch compares against exact values. It cannot test a range or relational condition.
Continue with Loops in Java and practise with our ICSE Class 9 Computer Applications Sample Paper.
Related Posts
Continue learning with these related articles

Math Library Methods in Java for ICSE Class 9 (with Outputs)
ICSE Class 9 Computer Applications notes on the Java Math class: sqrt, cbrt, pow, abs, max, min, ceil, floor, round, rint and random, with return types, tricky negative-number outputs and Java expressions for formulas.

Loops in Java for ICSE Class 9: for, while, do-while and Nested Loops
ICSE Class 9 Computer Applications notes on loops in Java: for, while and do-while loops, entry vs exit control, break and continue, nested loops and pattern programs, with dry runs, output questions and solved programs.

Constructors in Java: ICSE Class 10 Notes with Types, Overloading and Programs
ICSE Class 10 Computer Applications notes on constructors: what a constructor is, default and parameterised constructors, constructor overloading, the this keyword, differences from methods, and solved programs.