Java Control Flow (Conditionals and Loops)

Context: FIT2099_MOC · make decisions and repeat work in a Java method · branch (if/switch) and loop (while/for) Problem it solves: choose a branch by condition, or repeat a block a known or unknown number of times.

Quick Revision

  • 🎯 Trigger: decide → if/else, ternary, or switch; repeat → for/for-each (count known) or while/do-while (count unknown).
  • ⚡ Key Constraint: switch cases fall through without break; while loops need the loop variable updated or they run forever.

🔧 Minimal Working Example

if (operator == '+')      result = a + b;
else if (operator == '-') result = a - b;
else                      result = 0;          // catch-all
 
int min = (a < b) ? a : b;                      // ternary: condition ? ifTrue : ifFalse

Expected output: result set by the matching branch; min = the smaller of a, b.

  • if / else if / else ➔ the general branch; the final else is the catch-all.
  • Ternary(cond) ? valueIfTrue : valueIfFalse — a one-line if/else for an assignment.
  • switch ➔ dispatch on one value; each case needs a break; default is the catch-all.
  • Loopswhile (cond) {} · do {} while (cond) (runs ≥1) · for (init; cond; update) {} · for (T x : coll) {}.

🔀 Variations

  • switch (needs break)
switch (operator) {
    case '+': result = a + b; break;
    case '-': result = a - b; break;
    default:  result = 0;     break;
}
  • for vs for-eachfor (int i = 0; i < xs.length; i++) when you need the index; for (String s : xs) to just visit each element.
  • do-while ➔ runs the body before testing — ideal for “keep asking until valid input”.

✍️ Practice

⚠️ Common Mistakes

  • 💡 Missing break = fall-through ➔ without break, a matched case runs into every case below it.
  • 💡 Infinite while ➔ forget to update the loop variable (i++) and the condition never becomes false.
  • 💡 Which loop? ➔ count known ➔ for/for-each; count unknown (wait for a condition) ➔ while/do-while.