Dart Loops

In this tutorial you will learn about the Dart Loops and its application with practical example.

Dart Loops

Loop statements are used to execute the block of code repeatedly for a specified number of times or until it meets a specified condition. Loop statement are very useful to iterate over collection/list of items or to perform a task for multiple times. In Dart, we have following loop statements available-

Dart for Loop

The for loop is used when we want to execute block of code known times. In Dart, basic for loop is similar as it is in C. The for loop takes a variable as iterator and assign it with an initial value, and iterate through the loop body as long as the test condition is true.

Syntax :-

Example:-

Output:-

dart_for_loop_example

Dart for..in Loop

The for in loop takes an expression or object as iterator, and iterate through the elements one at a time in sequence. The value of the element is bound to var, which is valid and available for the loop body. Once the loop statements are executed current iteration, the next element is fetched from the iterator, and we loop another time. When there is no more elements in iterator, the for loop is ended.

Syntax:-

Example:-

Output:-

dart_for_in_loop_example

Dart While Loop

The while loop will execute a block of statement as long as a test expression is true.

Syntax:-

Example:-

Output:-

dart_while_loop_example

Dart do…while Loop

The do…while statement executes loop statements and then test the condition for next iteration and executes next only if condition is true.

Syntax:-

Example:-

Output:-

dart_do_while_loop_example

In this tutorial we have learn about the Dart Loops and its application with practical example. I hope you will like this tutorial.