2 · Control Flow & Methods · lesson 7 of 41

if / else

Branching with boolean expressions.

Java
if (score >= 90)      grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else                  grade = 'F';

String label = (age >= 18) ? "adult" : "minor";  // ternary
Key takeaways
  • ✓Conditions must be boolean.
  • ✓The ternary ?: is a compact if/else expression.