Looping statement in Dart or any other programming language is used to repeat a certain set of commands until certain conditions are not completed. There are different ways to do so. They are:
- for loop
- for… in loop
- while loop
- do while loop
for loop:
For loop in Dart is similar to that in Java and also the flow of execution is the same as that in Java.
Syntax:
for(initialization; condition; test expression){
// Body of the loop
}
Control flow:
Control flow goes as:
- initialzation
- condition
- body of loop
- test expression
The first is executed only once i.e in the beginning while the other three are executed until the condition turns out to be false.
Example:
// Printing GeeksForGeeks 5 times void main() { for (int i = 0; i < 5; i++) { print('GeeksForGeeks'); } } |
Output:
GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks GeeksForGeeks
for…in loop:
For…in loop in Dart takes an expression or object as an iterator. It is similar to that in Java and its flow of execution is also the same as that in Java.
Syntax:
for (var in expression) {
// Body of loop
}
Example:
void main() { var GeeksForGeeks = [ 1, 2, 3, 4, 5 ]; for (int i in GeeksForGeeks) { print(i); } } |
Output:
1 2 3 4 5
while loop:
The body of loop will run until and unless condition is true.
Syntax:
while(condition){
text expression;
// Body of loop
}
Example:
void main() { var GeeksForGeeks = 4; int i = 1; while (i <= GeeksForGeeks) { print('Hello Geek'); i++; } } |
Output:
Hello Geek Hello Geek Hello Geek Hello Geek
do..while loop:
The body of the loop will be executed first and then the condition is tested.
Syntax:
do{
text expression;
// Body of loop
}while(condition);
Example:
void main() { var GeeksForGeeks = 4; int i = 1; do { print('Hello Geek'); i++; } while (i <= GeeksForGeeks); } |
Output:
Hello Geek Hello Geek Hello Geek Hello Geek
Recommended Posts:
- How to Combine Lists in Dart?
- Basics of Numbers in Dart
- Dart - Super and This keyword
- Dart - Standard Input Output
- Generators in Dart
- Dart - Classes And Objects
- Operators in Dart
- Switch Case in Dart
- Dart - Loop Control Statements (Break and Continue)
- Dart Programming - Functions
- Instance and class methods in Dart
- Abstract Classes in Dart
- Labels in Dart
- Dart Programming - List
- Different Types of Functions in Dart
- Dart - Splitting of String
- Method Overriding in Dart
- Concept of Isolates in Dart
- Concept of Callable Classes in Dart
- Getter and Setter Methods in Dart
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

