Skip to main content

Control Flow

Conditional Control Statements

Complete lesson for Conditional Control Statements using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Conditional Control Statements

Section: Control Flow. if, else-if, nested decisions, classic switch, and Java 14+ switch expressions.

if, else if, else

  • The condition must be boolean — you cannot write if (x) for an int like C/C++.
  • Braces are optional for one statement but you should always use them to avoid dangling-else bugs.
public class GradeIf {
    public static void main(String[] args) {
        int marks = 76;
        if (marks >= 90) {
            System.out.println("A");
        } else if (marks >= 75) {
            System.out.println("B");
        } else {
            System.out.println("C or below");
        }
    }
}

Classic switch (statement)

  • Supported selector types: byte, short, char, int and their wrappers, String (Java 7+), and enums.
  • long, float, double, and boolean cannot be switch selectors.
  • Without break, execution falls through to the next case — usually a bug, sometimes intentional.

Switch expressions (Java 14+)

  • Arrow cases (case 1 ->) do not fall through. A switch can produce a value.
  • yield is used in a block case to produce the value. default is required when the switch is used as an expression (unless the enum is exhaustive).
StyleFall-throughProduces a value?
switch statement + breakYes, if you omit breakNo (side effects only)
switch expression + ->NoYes
switch expression + {} / yieldNoYes
L2

Advanced — Conditional Control Statements

Nested decisions and pattern matching

  • Deeply nested if is hard to test — extract methods or use a switch/table.
  • Java 16+ if (obj instanceof String s) binds s if the test succeeds (pattern matching for instanceof).
  • Java 17 preview/21+ expands switch pattern matching; interviews for Java 17 still focus on classic + arrow switch.
L3

Tricky Interview Points

Q1) Can you write if (1) in Java?

Answer No. The condition must be boolean. if (1) is a C/C++ habit and does not compile.

Q2) Which types work in a switch?

Answer byte, short, char, int (and wrappers), String, and enum. Not long, float, double, or boolean.

Q3) What is fall-through?

Answer In a classic switch, after a matching case, execution continues into later cases until break, return, or the end.

Q4) What did Java 14 add for switch?

Answer Switch expressions: arrow cases without fall-through, and the ability to return a value (yield in block cases).

Q5) Is default mandatory?

Answer For a switch statement, no. For a switch expression, yes — unless the compiler can prove every enum constant is covered.

Q6) Can two cases share a body?

Answer Yes. Classic: stacked case labels. Arrow switch: case 1, 2, 3 -> ...

L4

Simple Programs

Switch expression (Java 14+)

public class DayType {
    public static void main(String[] args) {
        String day = "SAT";
        String kind = switch (day) {
            case "SAT", "SUN" -> "weekend";
            case "MON", "TUE", "WED", "THU", "FRI" -> "weekday";
            default -> "unknown";
        };
        System.out.println(kind);
    }
}

Classic switch with break

public class ClassicSwitch {
    public static void main(String[] args) {
        int n = 2;
        switch (n) {
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            default:
                System.out.println("other");
        }
    }
}
L5

Problem Solving Tricks

  • Always use braces on if/else.
  • Prefer arrow switch when you only need a value — no forgotten break.
  • Switch on enums instead of magic strings when the set is closed.
  • Put the most common / cheapest condition first in an else if chain only if it stays readable.
L6

Practice Programs List

  1. Map marks 0–100 to letter grades with else if.
  2. Rewrite that mapping as a switch expression over marks / 10.
  3. Write a classic switch that *intentionally* falls through (e.g. days in a month) and comment why.
  4. Reject if (x = 5) vs if (x == 5) and explain the compile error for assignment in a boolean context.
  5. Use instanceof pattern matching to print the length if an Object is a String.