Memra

if / else if / else

Branching on boolean conditions.

Making decisions

if (score >= 90) {
    grade = 'A';
} else if (score >= 80) {
    grade = 'B';
} else {
    grade = 'C';
}

The condition must be a boolean — unlike C, Java won't accept if (x) where x is an int. Branches are tested top to bottom; the first true one runs and the rest are skipped.

Always use braces { }, even for one-line bodies. Brace-less if statements are a notorious source of bugs (the "goto fail" class of error), and the cost of the braces is two characters.

NORMAL ~/memra/learn/java-from-zero/if-else utf-8 LF