Control flow¶
Selection¶
if (temperature > 30) {
System.out.println("hot");
} else if (temperature < 10) {
System.out.println("cold");
} else {
System.out.println("mild");
}
A traditional switch statement uses labels and explicit transfer:
A switch expression returns a value and avoids accidental fall-through:
Iteration¶
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
for (int value : values) {
System.out.println(value);
}
while (ready()) {
process();
}
A do/while loop executes its body at least once.
Transfer¶
breakexits a loop or switch.continueskips to the next loop iteration.returnexits the current method, optionally returning a value.throwexits normal control flow by raising an exception.
try, catch, and finally participate in exception handling; final is a modifier, not a transfer statement.
Repository examples¶
src/main/java/nitin/a4flowControl/F1switch.javasrc/main/java/nitin/a4flowControl/SwitchExpressions.javasrc/main/java/nitin/a4flowControl/FlowControl.md