Skip to content
Conditionals
step 1/5

Reading — step 1 of 5

Learn

~3 min readControl Flow

Conditionals

Programs make decisions with if / else if / else. Java's rule is stricter than Python's or JavaScript's, and it's strict on purpose: the condition must be a boolean. Not a number, not a string, not "anything truthy" — a real true-or-false value.

int score = 87;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("F");
}

Java walks the chain top-down and runs the FIRST branch whose condition is true — then skips the rest. score = 87 prints only B.

Why booleans only?

The classic C bug is typing = (assignment) where you meant == (comparison). In C, if (x = 5) compiles and is always true. In Java it's a compile error — x = 5 evaluates to an int, and an int isn't a boolean. A whole category of bugs, deleted by the type system:

if (n == 5) { ... }   // comparison — correct
if (n = 5) { ... }    // error: incompatible types: int cannot be converted to boolean

Combining conditions

if (age >= 18 && hasId) { ... }      // AND — both must be true
if (day == 6 || day == 7) { ... }    // OR — at least one
if (!done) { ... }                   // NOT — flips it

&& and || short-circuit: if the left side already decides the answer, the right side never runs. if (s != null && s.length() > 0) is safe precisely because the length check is skipped when s is null.

One more Java-specific rule: compare String contents with .equals("yes"), never ==== compares object identity, not text (the Strings lesson covers why).

Order is logic

In an else if chain, the ORDER of tests is part of your logic. Put the most specific condition first:

// CORRECT — most specific first:
if (n % 15 == 0) {
    System.out.println("FizzBuzz");
} else if (n % 3 == 0) {
    System.out.println("Fizz");
} else if (n % 5 == 0) {
    System.out.println("Buzz");
} else {
    System.out.println(n);
}
// BROKEN — n = 15 matches % 3 first and prints "Fizz".
// The FizzBuzz branch below is unreachable:
if (n % 3 == 0) {
    System.out.println("Fizz");
} else if (n % 15 == 0) {
    System.out.println("FizzBuzz");   // never runs!
}

A number divisible by 15 is also divisible by 3, so the general test steals the match from the specific one. This exact bug is the most common failed submission on this lesson's exercise.

(Java 14+ also has an arrow-form switch expression — case 8 -> "B"; — worth recognizing in modern code. For this exercise, a plain if chain is all you need.)

Your exercise

Read one integer and print:

  • FizzBuzz if divisible by 15
  • Fizz if divisible by 3
  • Buzz if divisible by 5
  • the number itself otherwise

Use the four-branch chain above — test % 15 FIRST. The grader's first test feeds you 15 and expects FizzBuzz; if you test % 3 before % 15, it prints Fizz and fails immediately. Capitalization counts too: FizzBuzz with capital F and capital B, no space.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Conditionals — Java Fundamentals