Skipping Iterations with continue
メニューを表示するにはスワイプしてください
The continue keyword allows you to skip the remaining code within a loop for the current iteration and proceed to the next iteration.
Example 1: Skipping Early Iterations
1234567for (let i = 0; i < 10; i++) { if (i < 5) { continue; } console.log("i =", i); }
In this example, the continue keyword is used to skip iterations of the loop where i is less than 5. As a result, only iterations with i values in the range [5, 9] execute the code inside the loop.
Example 2: Skipping a Specific Iteration
123456789for (let i = 1; i <= 5; i++) { console.log("Iteration started:", i); if (i == 3) { continue; // Skip the end of the 3rd iteration } console.log("- Iteration ended:", i); }
Here, the continue statement is used to skip the end of the 3rd iteration, resulting in the output of "Iteration started" and "Iteration ended" messages for all iterations except the one where i equals 3.
Note
The
continuekeyword works similarly tobreak, but instead of terminating the loop entirely, it only skips the current iteration and proceeds with the next one. This behavior can be handy for fine-grained control of loop execution.
フィードバックありがとうございます!
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください