4. Dart Loops

Loops are an ideal way repeat the execution of the code over again and again. Loops are a set of instructions executed when we must repeat the certain things. Iteration is a repetition of the context/code. It is the ideal way of repeating a block of code without rewriting it again and again , the code executes when the specified code is met..

Loops in Dart

Dart loops is divided into two types:- For , while and do-while let’s us overiview the loops in dart: For Example

void main(){
  print("Hello World");
  print("Hello World");
  print("Hello World");
}

Output

Hello World
Hello World
Hello World

If loop is introduced than above program can be written as :

void main(){
  for( int i=0; i<=2 ; i++){
    print("Hello World");
  }
}

Output

Hello World
Hello World
Hello World

Compare the Ouput do you see any changes, NO. The above both codes produces a same output how by loop. See the example , what if we want to write “Hello World” 100 times than what will you do write print 100 times or simply change the number of iteration to 100. You will change iteration to 100 because it’s the easiest way to do it. That is why loops are introduce.

code Try Yourself