Programs often need to choose between actions depending on conditions. Java provides the if and switch statements for this.

The if Statement

The simple if executes a statement or block only when a condition is true: if (marks >= 40) System.out.println("Pass");. The if-else statement chooses between two alternatives. A nested if-else places one if-else inside another to test several conditions in sequence. The else-if ladder tests a series of conditions in order and executes the block of the first true one, with a final else for all other cases. It is used for grading and slabs of tax or charges. The condition must be a boolean expression; unlike C, Java does not treat 0 and 1 as false and true.

Definition: Decision-making statement. A statement that chooses which statements to execute according to the value of a condition, such as if, if-else and switch.

The switch Statement

The switch statement selects one of many blocks according to the value of an expression. The expression may be of type int, byte, short, char, String or an enum. Each case label gives a constant value, and the code after the matching case runs. The break statement ends the switch; without it, execution falls through to the next case. The default case runs when no case matches. switch is clearer than a long else-if ladder when one variable is compared with many fixed values. Newer Java versions also offer switch expressions with arrows (case 1 -> ...), which do not fall through.

The Conditional Operator

For simple two-way choices that produce a value, the ?: operator is a compact alternative to if-else.

Table 2.5: Forms of decision making

Statement Use Example
if Do something when true Print "Pass"
if-else Choose one of two Even or odd
else-if ladder Choose one of many by ranges Grade from marks
switch Choose one of many by values Day name from number
?: Choose a value max = a > b ? a : b

Example 2.5: Grade by else-if and day by switch

Problem: Write code that gives a grade for 78 marks (A for 80 and above, B for 60 to 79, C for 40 to 59, F below 40), and the day name for day number 3.

Solution:

  1. The else-if ladder tests marks >= 80 (false), then marks >= 60 (true), giving B.
  2. The switch on 3 matches case 3, giving Wednesday, and break ends it.

Result: Grade B and day Wednesday.

public class Decisions {
    public static void main(String[] args) {
        int marks = 78, day = 3;
        char grade;
        if (marks >= 80) grade = 'A';
        else if (marks >= 60) grade = 'B';
        else if (marks >= 40) grade = 'C';
        else grade = 'F';
        System.out.println("Marks " + marks + " -> grade " + grade);
        String name;
        switch (day) {
            case 1: name = "Monday"; break;
            case 2: name = "Tuesday"; break;
            case 3: name = "Wednesday"; break;
            default: name = "Other day";
        }
        System.out.println("Day " + day + " is " + name);
        System.out.println(15 % 2 == 0 ? "15 is even" : "15 is odd");
    }
}

Output

Marks 78 -> grade B
Day 3 is Wednesday
15 is odd

Common mistakes

  • Leaving out break in a switch, causing fall-through to the next case.
  • Putting a semicolon after if (condition);, which ends the if at once.
  • Using an integer such as 1 as a condition. Java needs a boolean.

Key takeaways

  • if, if-else, nested if and else-if ladders handle conditions.
  • switch selects by value; break prevents fall-through; default handles the rest.
  • switch works with int, char, String and enum.
  • Conditions must be boolean.

Practice questions

  1. What happens if break is omitted in a switch case?
  2. Which types can a switch expression have?
  3. When is an else-if ladder preferred to switch?

Answers to the practice questions

  1. Execution falls through into the next case.
  2. int, byte, short, char, String or enum.
  3. When conditions test ranges rather than fixed values.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit II