Dart While Loop

Dart While loop statement is used to execute a block of code repeatedly in a loop based on a condition.

As long as the condition is true, execution runs the block of statements inside the while loop. If the condition is false, execution breaks the loop and continues with the rest of the statements in program.

In this tutorial, we will learn the syntax to write while loop statement in Dart programming language, and how to use while loop with the help of example programs.

Syntax

The syntax of while loop is

while (condition) {
  // statement(s)
}

The statement(s) are executed repeatedly in loop, as long as the condition is True. The condition is a boolean expression that evaluates to a boolean value.

Flowchart

The following is a flowchart to the flow of execution of while loop statement. The loop is depicted in blue lines.

Dart While Loop

Example

In the following program, we have a while loop statement that prints 'Hello World' five times.

main.dart

void main() {
  var i = 0;
  while (i < 5) {
    print('Hello World');
    i++;
  }
}

Output

Hello World
Hello World
Hello World
Hello World
Hello World

Nested While Loop

The body of while loop consists of statements. And one or more of these statements can be while loop, making it a nested while loop.

In the following example, we will write a nested while loop statement to print ten integer multiples of 1 to 5 from 1 to 10.

main.dart

import 'dart:io';

void main() {
  var i = 1;
  while (i <= 5) {
    var j = 1;
    while (j <= 10) {
      stdout.write(i * j);
      stdout.write('  ');
      j++;
    }
    stdout.write('\n');
    i++;
  }
}

Output

1  2  3  4  5  6  7  8  9  10  
2  4  6  8  10  12  14  16  18  20  
3  6  9  12  15  18  21  24  27  30  
4  8  12  16  20  24  28  32  36  40  
5  10  15  20  25  30  35  40  45  50

While Loop with Break

Break statement can be used inside a looping statement to break the loop even before condition becomes false.

In the following example, we have a while loop statement. The while loop has a break statement that executes conditionally when i is 4. And inside this if-statement we have break statement. This break statement breaks the while loop.

If there is no break statement, then the while loop would have executed until i = 10.

main.dart

void main() {
  var i = 1;
  while (i <= 10) {
    print(i);
    if (i == 4) break;
    i += 1;
  }
}

Output

1
2
3
4

While Loop with Continue

Continue statement can be used to skip the execution of further statements in while loop body for that cycle, and continue with next iteration of while loop.

In this example, we shall write a program with while loop that prints numbers from 1 to 10. But, when we get an odd number, we will continue the loop with next iterations.

main.dart

void main() {
  var i = 0;
  while (i <= 10) {
    i += 1;
    if (i % 2 == 1) continue;
    print(i);
  }
}

Make sure that you update the control variables used in the while loop condition, before you execute the continue statement.

Output

2
4
6
8
10

Infinite While Loop

Infinite while loop is a loop in which the condition is always true and never comes out of the loop.

main.dart

void main() {
  while (true) {
    print('Hello World');
  }
}

Output

Hello World
Hello World
Hello World
Hello World

The execution prints the string 'Hello World' indefinitely. Only part of the output is provided above.

Conclusion

In this Dart Tutorial, we learned what a while loop is, its syntax, working mechanism of while loop, nested while loop and much more with examples.