Dart

Dart for-in Loop

Dart for-in Loop

The Dart for-in Loop is different from the normal Dart for loop. This loop is normally used to iterate over the elements in a collection object.

for-in Loop Syntax

The general syntax of Dart for-in Loop is as follows:
for (var loop_variable in expression) {
  //code statement(s)
}
The loop takes a Dart object or expression as an iterator and iterates over the elements one at a time. The value of the element is bound to var, which is valid and available within the scope of the loop body.

Example

Let’s understand the concept using an example. We need to declare the iterator variable to get the element from the iterator. The loop will execute until no element is left in the iterator.
/*
Dart for-in statement demo Program
Dart Tutorials - www.TestingDocs.com
*/
void main()
{
var list=["Apple","Grapes","Cucumber","Orange"];

//for-in Loop to print the list elements
  for(var e in list) {
      print(e); // print the list element
  }
}

Dart Program Output

Apple
Grapes
Cucumber
Orange

 

Dart for-in Loop Program

 

—

Dart Tutorials

Dart tutorial on this website can be found at: