Control flow and loops, step by step

Decide

Only when a condition holds

ifelseswitch

Repeat

One block, many times

forwhiledo-while

Jump

Cut or skip laps

breakcontinue
nota = 85
nota >= 90
Excellent
nota >= 70
Passed
else
Failed
nota = 85
(nota >= 70)

condition

?
"Passed"

if true

:
"Failed"

if false

resultado = "Passed"
dia = 3
case 1β†’Monday
case 2β†’Tuesday
case 3β†’Wednesdaybreak
default→Invalid day

Without break, execution falls into the next case

case 1 ->"Monday"
case 2 ->"Tuesday"
case 3 ->"Wednesday"
default ->"Invalid day"

dia = 3

nombreDia = "Wednesday"

Console menu

1Add personcargarPersona()
2List peoplelistarPersonas()
0Quitprintln("…")
defaultInvalid optionprintln("…")
case 1 -> { … }
for

Laps known in advance

start Β· condition Β· step
while

Checks before the block

may never run
do-while

Checks after the block

at least once

Start

int i = 1

Condition

i <= 5

Step

i++
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6

6 <= 5 false: exits

while (contador < 3)
contador = 0

0 < 3 true

contador = 1

1 < 3 true

contador = 2

2 < 3 true

contador = 3

3 < 3 false: exits

Block

runs first

numero < 5

checked at the end

10 < 5 = false

the loop ends

The block ran once, even though the condition was false
1 … 100 even only: 2, 4, 6 … suma = ?

Result

suma = 2550