Loops and Ranges
Continue in Loops
Skip the rest of one iteration.
continue-loop
`continue` skips to the next iteration. It is useful when one value should not participate in the rest of the loop body.
Continue in Loops
continue_loop.js
Replay: real traced execution (multi-file project)
const skip = 2;
let total = 0;
for (let i = 1; i <= 5; i += 1) {
if (i === skip) {
continue;
}
total += i;
}
console.log("skip=" + skip);
console.log("total=" + total);
const skip = 3;
let total = 0;
for (let i = 1; i <= 5; i += 1) {
if (i === skip) {
continue;
}
total += i;
}
console.log("skip=" + skip);
console.log("total=" + total);
const skip = 5;
let total = 0;
for (let i = 1; i <= 5; i += 1) {
if (i === skip) {
continue;
}
total += i;
}
console.log("skip=" + skip);
console.log("total=" + total);
skip ← 2, total ← 0
1const skip→ 2 = 2; //@skip=3, 52let total→ 0 = 0;3for (let i = 1; i <= 5; i += 1) {total ← 1
pass 1 of 51const skip = 2; //@skip=3, 52let total = 0;3for (let i1 = 1; i <= 5; i += 1) {4 if (i === skip) {5 continue;6 }7 total += i1;8}values this step1totalAll 5 passes — pass 1 is the card above pass iskiptotal1 1 — 1 2 2 2 — 3 3 — 4 4 4 — 8 5 5 — 13 if (i === skip)
2let total = 0;3for (let i = 1; i <= 5; i += 1) {4 if (i2 === skip2) {5 continue;6 }console.log("skip=" + skip);
7 total += i;8}910console.log("skip=" + skip2);11console.log("total=" + total13);outputskip=2 total=13
skip ← 3, total ← 0
1const skip→ 3 = 3;2let total→ 0 = 0;3for (let i = 1; i <= 5; i += 1) {total ← 1
pass 1 of 51const skip = 3;2let total = 0;3for (let i1 = 1; i <= 5; i += 1) {4 if (i === skip) {5 continue;6 }7 total += i1;8}values this step1totalAll 5 passes — pass 1 is the card above pass iskiptotal1 1 — 1 2 2 — 3 3 3 3 — 4 4 — 7 5 5 — 12 if (i === skip)
2let total = 0;3for (let i = 1; i <= 5; i += 1) {4 if (i3 === skip3) {5 continue;6 }console.log("skip=" + skip);
7 total += i;8}910console.log("skip=" + skip3);11console.log("total=" + total12);outputskip=3 total=12
skip ← 5, total ← 0
1const skip→ 5 = 5;2let total→ 0 = 0;3for (let i = 1; i <= 5; i += 1) {total ← 1
pass 1 of 51const skip = 5;2let total = 0;3for (let i1 = 1; i <= 5; i += 1) {4 if (i === skip) {5 continue;6 }7 total += i1;8}values this step1totalAll 5 passes — pass 1 is the card above pass iskiptotal1 1 — 1 2 2 — 3 3 3 — 6 4 4 — 10 5 5 5 — if (i === skip)
2let total = 0;3for (let i = 1; i <= 5; i += 1) {4 if (i5 === skip5) {5 continue;6 }console.log("skip=" + skip);
7 total += i;8}910console.log("skip=" + skip5);11console.log("total=" + total10);outputskip=5 total=10