SLIDE 1 / 6

1. Exclusive Branching: if, else if, else

if / else if branches evaluate sequentially downwards. As soon as one condition evaluates to true, its block executes and all remaining branches are skipped.

edad < 13 ? "Infantil" edad < 18 ? "Juvenil" "Adulto"
Branching Simulator Adjust age value
16
Selected branch: else if (edad < 18)
Console output: "Categoría Juvenil"

When neither condition matches, the thread of execution falls naturally into the catch-all else clause.

2. The Ternary Operator: Inline Conditional

The ternary operator (? :) is a compact expression yielding one of two values based on a boolean condition. Unlike if statements, it evaluates to an assignable value.

saldo >= 0 Boolean condition ? "Al día" : "Deudor" String estado = (saldo >= 0) ? "Al día" : "Deudor";
Ternary Evaluator
Condition (balance >= 0): true
Result string assigned: "Al día"

Golden rule: Use ternaries only for simple, punchy decisions. Nesting ternaries ruins code readability.

3. Classic switch (Fall-through) vs Modern switch (->)

In classic switch statements, omitting break causes dangerous fall-through. Java 14 introduced arrow syntax (->) eliminating fall-through bugs by design.

Classic switch case 1: msg = "Uno"; // ¡FALTA BREAK! case 2: msg = "Dos"; break; Entering 1 falls through to 2 msg queda como "Dos" Modern Arrow Syntax case 1 -> "Uno"; case 2 -> "Dos"; default -> "Otro"; ✓ No fall-through possible Can return values directly
Fall-Through Demo
Input evaluated: dia = 1 (Lunes)
Final msg variable: "Martes" (¡Sobreescrito!)

Without a break, execution falls through to case 2 and overwrites "Monday". With arrow syntax, each branch is strictly isolated.

4. The 4-Step Clock of the for Loop

A for loop does not run all its clauses at once. It follows a rhythmic 4-step sequence: 1. Init (once), 2. Condition check, 3. Body, 4. Increment. Then loops back to step 2.

1. int i = 0; (Once at startup) 2. ¿i < 3? Checks boundary 3. Cuerpo del bucle println(i) 4. i++ Increments, loops to 2
Step-Through Simulator Paso 1 / 12
Active phase: 1. Inicialización (int i = 0)
Variable i: 0
Printed output: []

Click "Tick Step" to observe the clock hand visit each phase of the loop cycle.

5. while vs do-while: Where is the Turnstile?

In a while loop, the condition checks at the gate: if false, the body runs 0 times. In a do-while loop, execution runs first and validates at exit: guaranteed to run at least 1 time.

while (condicion) 🚪 Gate at ENTRANCE If initial condition is false: → Runs 0 TIMES while (!archivo.fin()) { ... } do { ... } while (condicion); Executes body first without checks 🚪 Gate at EXIT → Guaranteed 1+ TIMES Ideal for CLI menus
Live Comparison condicion = false
Iterations executed: 0
Best practice usage: Streams, sockets, buffers

Because the condition is false upfront, the while body is completely bypassed.

6. Loop Control: break vs continue

break immediately terminates the entire loop and jumps past it. continue skips only the remainder of the current iteration and advances straight to the next.

1 2 3 4 5 break en i == 3: Aborts the entire loop. Imprime: [1, 2] continue en i == 3: Skips 3 and continues. Imprime: [1, 2, 4, 5]

Control Flow Summary

  • if / else if: Sequential short-circuiting; executes strictly one branch.
  • switch con flechas (->): Eliminates fall-through bugs without needing break.
  • for de 4 tiempos: Init (1) -> Condition (2) -> Body (3) -> Increment (4).
  • do-while: Validated at exit gate; guarantees at least one execution.