Summary: in this tutorial, you’ll learn how to use the Dart continue
statement to start a new iteration of an enclosing loop.
Introduction to the Dart continue statement
The continue
statement allows you to skip the current iteration of the loop prematurely and start the next iteration immediately.
The continue
statement is only valid when you use it inside a loop, including while
, do while
, and for
loops.
The following shows the syntax of the continue
statement:
continue;
Code language: Dart (dart)
In practice, you’ll want to start the next iteration if a condition is true
. To do it, you use the continue
statement with an if
statement like this:
if (condition)
{
continue;
}
Code language: Dart (dart)
In this syntax, if the condition is true
, the continue
statement skips all the statements after the if
statement and starts the next iteration.
The following shows how to use the continue
statement in a while
loop:
while (expression)
{
if (condition)
{
continue;
}
// statements that will be skipped by
// the continue statement
}
Code language: Dart (dart)
In this syntax, if the condition is true
, the continue
statement will skip all the remaining statements underneath.
The following flowchart illustrates how the continue
statement works in a while
loop:
Likewise, you can use the continue
statement in a do while
statement:
do
{
if (condition)
{
continue;
}
// statements
} while (expression);
Code language: Dart (dart)
The following flowchart illustrates how the continue
statement works in a do while
loop:
And for
loop statement:
for (initializer; loopCondition; iterator)
{
if(condition)
{
continue;
}
// statements
}
Code language: Dart (dart)
If you use the continue
statement inside a nested loop, it’ll only skip the current iteration of the innermost loop.
The following flowchart shows how the continue
statement works in a for
loop:
Dart continue statement examples
The following example illustrates how the continue
statement works.
1) Using the Dart continue statement in a for loop
The following example uses the continue
statement in a for
loop to display only the odd numbers between 0 and 9:
void main() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
print(i);
}
}
Code language: Dart (dart)
Output:
1
3
5
7
9
Code language: Dart (dart)
How it works.
The for
loop iterates the numbers from 0 to 9 and assigns the number to the variable i
in each iteration.
If the current number (i
) is even, the continue statement skips the print statement.
However, if the current number (i) is odd, the condition in the if
statement is false
. Hence, the continue
statement is not reached and the print statement executes that displays the odd number.
2) Using the continue statement in a while loop example
The following example uses the continue
statement in a while
loop to calculate the total of odd numbers between 0 and 9:
void main() {
int total = 0, i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
total += i;
print(i);
}
print('Total: $total');
}
Code language: Dart (dart)
In this example:
- First, each iteration of the while loop will increase the value of the variable
i
by one untili
is 9. - Second, if the variable
i
is an even number, the condition of the if statement inside thewhile
loop will execute and skip the remaining statements after it. - Third, if the variable
i
is an odd number, it will add an odd number to the total variable. - Finally, display the total after the loop.
Summary
- Use the
continue
statement inside a loop to start the next iteration prematurely.