Loops in DART

In this article, I am going to explain how to use loops in DART.
  • 3694

Looping In DART

Dart provide different looping  mechanisms which allow us to execute a block of code repeatedly until a certain condition is met.

  • The for loop

    Dart provide  a shorthand mechanism for iterating through loop, and execute a statement in this loop until condition is true.

Example of for loop
 

void main() {

  for(int i=1;i<=5;i++){

    print("${i}");

  }
}


Output:

forloop.png

  • The for in loop

    for in loop contains collection of elements. It same as foreach loop in C# .

Example of for in loop
 

void main() {

  var collection=[1,2,3,4,5];

  for(var a in collection){

    print(a);

  }
}


Output: Same as above Example

  • The while loop

    Like the for loop, the while is pre test loop.

Example of while loop
 

void main() {

  int i=1;

  while(i<=5 ){

    print("${i}");

    i++;

  }
}


Output: same as above example

  • The do-while loop

    The do-while loop is post text version of while loop, means if condition is not true; then it must be executed once.

Example of do-while loop
 

void main() {

  var i=1;

  do{

   print(i);

    i++;

 }

  while(i<=5);

}

Output:

 forloop.png

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 
© 2020 DotNetHeaven. All rights reserved.