Dart Continue
Dart continue statement is used to skip the execution of subsequent statements inside loop after continue statement, and continue with the next iteration.
If continue statement is used in nested loops, only the immediate loop is continued with.
continue statement can be used inside a For loop, While loop and Do-while loop statements.
Syntax
The syntax of continue statement is
continue;
Examples
In the following examples, we will cover scenarios where we use continue statement in different kinds of loops.
Continue For Loop
In the following example, we write a For loop to print numbers from 1 to 7. But, inside the For loop, we execute continue statement when i
equals 5. Therefore, when i
equals 5, the print statement after continue statement are not executed for this iteration.
main.dart
void main() {
for (var i = 1; i < 8; i++) {
if (i == 5) {
continue;
}
print(i);
}
}
Output
1
2
3
4
6
7
5
is not printed in the output.
Continue While Loop
In this example, we print numbers from 1 to 7 using while loop. But when i=5
, we skip the iteration, continue with the next iteration.
main.dart
void main() {
var i = 0;
while (i < 7) {
i++;
if (i == 5) {
continue;
}
print(i);
}
}
Output
1
2
3
4
6
7
Continue Do-while Loop
In this example, we print numbers from 1 to 7 using do-while loop But when i=5
, we skip the iteration, continue with the next iteration.
main.dart
void main() {
var i = 0;
do {
i++;
if (i == 5) {
continue;
}
print(i);
} while (i < 7);
}
Output
1
2
3
4
6
7
Conclusion
In this Dart Tutorial, we have learnt Dart continue statement, and how to use this continue statement inside loop statements.