Loops & Switch - Exercise 3

continue skips the remaining statements in the current iteration and starts the next one. The loop's update still occurs for a for loop, so the condition can continue making progress.

for (let number = 0; number < 5; number++) {
  if (number % 2 === 0) continue;
  console.log(number); // 1, 3
}

break exits the nearest loop immediately, skipping all later iterations. It is useful when a search has found its answer or when continuing would do unnecessary work.

for (const name of ['Ava', 'Mina', 'Zoe']) {
  if (name === 'Mina') { console.log('Found'); break; }
}

The default case runs when no other case matches the switch expression. It provides a deliberate fallback for unexpected input instead of silently doing nothing.

const command = 'pause';
switch (command) {
  case 'play': console.log('Playing'); break;
  default: console.log('Unknown command');
}

The for...of loop in JavaScript is used to iterate over iterable objects like arrays, strings, maps, sets, etc. The syntax is:

javascript

for (const item of iterable) {
    // Code block to be executed
}

You can use a for...of loop in JavaScript to iterate over elements of an iterable object such as arrays, strings, maps, sets, etc.

javascript

const fruits = ['apple', 'banana', 'cherry'];
for (const fruit of fruits) {
    console.log(fruit);
}

A nested switch evaluates a second expression inside a matching outer case. This can model related choices, such as selecting a product first and then selecting its variant, but deeply nested logic may be clearer as a lookup object or function.

const type = 'fruit';
const color = 'red';
switch (type) {
  case 'fruit':
    switch (color) { case 'red': console.log('Apple'); break; }
    break;
}

You can use a nested switch statement in JavaScript by placing one switch statement inside another switch statement. This allows you to handle multiple conditions and execute corresponding blocks of code based on the values of those conditions.

javascript

let fruit = "apple";
let color = "red";

switch (fruit) {
    case "apple":
        switch (color) {
            case "red":
                console.log("It's a red apple");
                break;
            case "green":
                console.log("It's a green apple");
                break;
            default:
                console.log("Unknown color for apple");
        }
        break;
    default:
        console.log("Unknown fruit");
}

A switch has no iteration to continue, so continue does not move to another case. When the switch is inside a loop, it skips the rest of the current loop iteration and proceeds with the next iteration.

for (const value of [1, 2, 3]) {
  switch (value) { case 2: continue; default: console.log(value); }
}

In a switch, break exits the switch after the matching case. Without it, execution may fall through into later cases, so use it unless shared fall-through is intentional.

const answer = 'yes';
switch (answer) {
  case 'yes': console.log('Accepted'); break;
  default: console.log('Declined');
}

A switch selects one branch by comparing a single expression with fixed case values. It is a readable choice when one value determines several discrete outcomes; use default for everything else.

const key = 'save';
switch (key) {
  case 'save': console.log('Saving'); break;
  case 'load': console.log('Loading'); break;
  default: console.log('Unknown action');
}