Dart while Loop
Dart while Loop
In this tutorial, we will learn about the Dart while loop with an example. Dart while loop is used to repeatedly execute a block of statements as long as a specified condition is true.
Dart while Loop
The while loop is normally used when we don’t know the number of times to execute the code block. The while loop executes a block of code until the given expression is false.
The general syntax of a while loop is as follows:
while (condition) { // statements to be executed }
Condition
The condition is evaluated before each while loop iteration. If the condition is true, the while loop block is executed. If the condition is false, the while loop terminates. The while loop block can contain multiple statements to be executed.
Example
Let’s understand the following example. Dart program to print numbers from 1 to 10 using a while statement.
/* * While Loop demo Dart Program * Dart Tutorials - www.TestingDocs.com */ void main() { int i = 1; // Dart while Loop while(i <= 10) { print(i); i++; } }
Output
C:/tools/dart-sdk/bin/dart.exe –enable-asserts C:\Users\testingdocs\SampleDartProject\whileLoop.dart
1
2
3
4
5
6
7
8
9
10
Process finished with exit code 0
In this example, the while loop starts with variable i initialized to 1. It continues executing the while loop body as long as the variable i is less than or equal to 10. After each iteration, i is incremented by 1.
–
Dart Tutorials
Dart tutorial on this website can be found at:
https://www.testingdocs.com/dart-tutorials-for-beginners/
More information on Dart: