Loops & Switch - Exercise 5
A labeled break exits the loop named by the label, even when the statement is inside a nested loop. It is useful for ending a multi-level search as soon as the target is found.
outer:
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 3; column++) {
if (row === 1 && column === 1) break outer;
}
}
A labeled continue skips to the next iteration of the named outer loop. The inner loop ends immediately, so this is different from an unlabeled continue, which affects only the nearest loop.
rows:
for (let row = 0; row < 3; row++) {
for (let column = 0; column < 3; column++) {
if (column === 1) continue rows;
console.log(row, column);
}
}
An infinite loop never reaches a state that makes its condition false. In a browser it can block the main thread, so even a short demonstration should be commented out or stopped deliberately.
// Do not run without a planned exit:
// while (true) {
// console.log('still running');
// }
Make sure each iteration changes the value used by the loop condition, and test boundary cases such as an empty input. A guarded break can provide a second exit for searches or safety limits.
let attempts = 0;
while (attempts < 3) {
attempts++;
if (attempts === 2) break;
}
continue skips the rest of the current iteration and starts the next one. In a for loop, the update expression still runs, so the counter can move toward its stopping condition.
for (let number = 0; number < 4; number++) {
if (number === 2) continue;
console.log(number); // 0, 1, 3
}
The default case runs when no case value strictly matches the switch expression. Use it to provide a fallback message, validation result, or recovery action for unsupported input.
const size = 'xl';
switch (size) {
case 'm': console.log('Medium'); break;
default: console.log('Unsupported size');
}
You can use the default case in a switch statement to define the block of code that should be executed when none of the case expressions match the value of the switch expression.
let fruit = "banana";
switch (fruit) {
case "apple":
console.log("Apple is selected");
break;
case "orange":
console.log("Orange is selected");
break;
default:
console.log("Unknown fruit");
}
In a switch, omitting break lets execution continue into the following case blocks. This intentionally groups cases that share behavior, but accidental fall-through can execute unrelated code.
const dayType = 'Saturday';
switch (dayType) {
case 'Saturday':
case 'Sunday': console.log('Weekend'); break;
default: console.log('Weekday');
}
A break exits the switch immediately after the matching case finishes. Without it, execution falls through into later cases until another break or the end of the switch is reached.
const result = 'ok';
switch (result) {
case 'ok': console.log('Done'); break;
default: console.log('Retry');
}
continue does not move to the next case of a switch; it applies to an enclosing loop. When a switch appears inside a loop, it skips the rest of that loop iteration after the switch has selected its case.
for (const value of [1, 2, 3]) {
switch (value) {
case 2: continue;
default: console.log(value);
}
}