🔀 Control Structures

Conditional Constructs

⏱ 3 hr3 topicsInteractive
🎯 By the end: You can write programs that take different paths using if/else, choose between many options with switch-case, and build a simple menu.

Programs often need to decide what to do based on a condition — pass or fail, big or small. Java's conditional constructs make these decisions (ICSE Class 9).

1if, if-else and the ladder

A condition is a test that is true or false:

if (marks >= 33)
  System.out.println("Pass");
else
  System.out.println("Fail");

For several options, chain them in an if-else-if ladder — Java checks each in turn until one is true:

if (marks >= 90)      grade = 'A';
else if (marks >= 75) grade = 'B';
else if (marks >= 33) grade = 'C';
else                   grade = 'F';

A nested if is an if inside another if (a condition checked only when the outer one is true).

Key points
  • if runs code when a condition is true; if-else chooses between two paths.
  • An if-else-if ladder checks several conditions in turn until one is true.
  • A nested if is an if inside another if.

2switch-case

When you compare ONE variable against several fixed values, switch-case is cleaner than a long ladder:

switch (day) {
  case 1: System.out.println("Monday"); break;
  case 2: System.out.println("Tuesday"); break;
  default: System.out.println("Other day");
}
  • case labels the values to match.
  • break stops the switch after a case runs.
  • default runs if no case matches (like else).
Fall-through: if you forget break, execution "falls through" and runs the next case(s) too. Sometimes this is used on purpose to group cases, but usually a missing break is a bug.
Key points
  • switch-case compares one variable against fixed values — cleaner than a long ladder.
  • break stops the switch after a case; default runs when no case matches.
  • Forgetting break causes fall-through — the next cases run too.

3Menu-driven programs & exit

A menu-driven program shows the user a list of choices, reads their choice, and uses switch-case (or a ladder) to do the matching task:

System.out.println("1. Add  2. Subtract  3. Exit");
int choice = sc.nextInt();
switch (choice) {
  case 1: System.out.println(a + b); break;
  case 2: System.out.println(a - b); break;
  case 3: System.exit(0);            break;   // ends the program
  default: System.out.println("Invalid choice");
}

System.exit(0) immediately stops the whole program. The 0 means it ended normally.

Key points
  • A menu-driven program lists choices, reads the user's choice, and runs the matching task (often with switch).
  • System.exit(0) immediately ends the whole program; 0 means a normal exit.
  • Always include a default for invalid choices.

★ Practical: decisions in code

In BlueJ, write programs that:

  1. Read a number and print whether it is positive, negative or zero (if-else-if).
  2. Read marks and print a grade using an if-else-if ladder.
  3. Read 1–7 and print the day of the week using switch-case with default.
  4. Build a small menu (Add / Subtract / Exit) that uses System.exit(0).

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.