← Skill tree CS Skill Tree 0 CSCD210

Conditionals: if, else, and switch

Textbook: BJP (Reges and Stepp)

Where you are: Week 0 review > Conditionals: if, else, and switch

Try This First

With score equal to 72, predict what this prints before reading on:

if (score >= 90) { grade = "A"; }
else if (score >= 80) { grade = "B"; }
else if (score >= 70) { grade = "C"; }
else { grade = "F"; }
System.out.println(grade);
Reveal

C. Java checks the conditions in order. The first two are false; the third (score >= 70) is true, so that branch runs and the chain stops. An else if chain runs exactly one branch.

Before You Start

Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.

If any of the above felt shaky, start with Variables and the Primitive Types first.

Not sure? Take the 60-second self-check.

Try each from memory, then read the answer under it.

  1. What does = mean in x = x + 1;? It means gets. The right side is computed first, then stored into the name on the left.
  2. Which primitive type holds only true or false? boolean.

What You Need To Walk In With

Walk into the next class able to state these:

You should be able to: trace a chain and name the branch that runs, and choose between if / else if and switch for a given task.

How It Works

The if / else if / else chain

An if runs its body when the boolean condition is true. An else captures the false case. Chain else if for three or more mutually exclusive outcomes:

if (n > 0) {
    System.out.println("positive");
} else if (n < 0) {
    System.out.println("negative");
} else {
    System.out.println("zero");
}

A chain says “these cases are mutually exclusive; pick exactly one.” Independent if statements say “check each separately; more than one body may run.” The difference shows the moment a body changes the variable being tested:

// Chain: exactly one branch fires
if (n > 0) { System.out.println("pos"); }
else if (n < 0) { System.out.println("neg"); }

// Independent: each if is its own decision; two may print
if (n > 0) { System.out.println("pos"); }
if (n < 0) { System.out.println("neg"); }

When to use switch

Use switch to test a single expression for equality against fixed constants: a day number, a menu command, a String code, an enum value. The arrow form is the one to write:

String dayName = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    default -> "unknown";
};

The arrow form has no fall-through: each arm is independent. switch does not accept double, float, or boolean, and it cannot express a range or a compound condition. When the test is score >= 90 or a > b && c != 0, reach for if / else if.

Situation Right construct
Ranges (>= 90, >= 80) if / else if
Equality against fixed values (day == 1, day == 2) switch (arrow form)
Three or more fixed values that produce a result switch expression

Worked Example: Predict, Then Check

int n = 95;
if (n >= 90) { System.out.println("A"); }
if (n >= 80) { System.out.println("B"); }
if (n >= 70) { System.out.println("C"); }

These are three separate if statements with no else. Predict how many lines print.

Reveal

Three lines: A, B, C. Without else, each condition is tested on its own, and 95 passes all three. A chain (else if) would have printed only A.

A Common Mistake

A frequent error is reaching for switch on a range, such as letter grades from a numeric score. switch matches a value against fixed constants; it cannot say “90 or above.” A score-to-grade test belongs in an if / else if chain. (Source: BJP (Reges and Stepp), Ch 4; JLS 25, §14.11 and §15.28.)

Go Deeper (optional)

For the curious: the older classic switch (with case 1: and break;) falls through to the next case when break is missing, which is a classic source of bugs. The arrow form (case 1 -> ...) removed that trap, and a switch can now produce a value directly (a switch expression). Writing the arrow form by default avoids the fall-through problem entirely.

Check Yourself

Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.

Check your understanding

With score = 72, the chain (A>=90, B>=80, C>=70, else F) prints what?

Tier 1 · BJP (Reges and Stepp), Ch 4

Three separate if statements with no else (>=90, >=80, >=70). With score = 95, how many lines print?

Tier 2 · BJP (Reges and Stepp), Ch 4

Which is a legal switch selector?

Tier 1 · BJP (Reges and Stepp), Ch 4

A test reads score >= 90 . Which construct fits?

Tier 2 · BJP (Reges and Stepp), Ch 4

In the arrow form case 1 -> ... , what happens after an arm runs?

Tier 3 · JLS 25, 15.28