Dart Loops for Flutter

Dart has common loops like Other Programming Language. Dart has For, While, ForEach Loops.

For Loop :

for (var i = 1; i <= 10; i++) {
  sum += i;  
}
print("The sum is $sum"); // The sum is 55

For Each Loop :

var numbers = [1, 2, 3, 4];
for (var number in numbers) {
  print(number);
}
// 1
// 2
// 3
// 4
numbers.forEach((number) => print(number));
// 1
// 2
// 3
// 4

While Loop :

while (i < 10) {
  print(i);
  i++;
}
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9

Do – While Loop :

i = 1;
do {
  print(i);
  ++i;
} while (i < 10);
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *