continue
The continue statement skips the rest of the current loop iteration and proceeds to the next one.
Syntax
continue;Usage
Use continue inside while, for, or for-in loops to skip certain iterations based on a condition.
Example: Skip even numbers
class App {
@unsafe for (let i = 0; i < 10; i = i + 1) {
if (i % 2 == 0) {
continue; // skip even numbers
}
print(i); // only prints odd numbers: 1, 3, 5, 7, 9
}
}Example: for-in with continue
class App {
let items = ["apple", "", "banana", "", "cherry"];
for (item in items) {
if (item == "") {
continue; // skip empty strings
}
print("Item:", item);
}
// Output: Item: apple
// Item: banana
// Item: cherry
}Example: while loop with continue
class App {
let i = 0;
@unsafe while (i < 10) {
i = i + 1;
if (i < 5) {
continue; // skip first 4 iterations
}
print(i); // prints: 5, 6, 7, 8, 9, 10
}
}Difference from break
| Statement | Effect |
|---|---|
break | Exits the loop completely |
continue | Skips to next iteration |
See also
- break — exit loop completely
- while loops
- for loops