Dart Tutorial : For Loop in Dart

In computer programming, when we need to repeat a given section of code a certain number of times until a particular condition is met, we use a loop. This is a control structure that is repeated until a certain condition is met.

The for loop is an implementation of a definite loop. The for loop executes the code block a specified number of times. It can be used to iterate over a fixed set of values, such as an array.



The general syntax of the for loop looks like this:


for(var x = 0; x <= 10; x++){
  //iteration from 0 to 10 happens in between
}

In the previous code, the value of x starts at 0.

Then we test if the loop is going to execute (x <= 10;). 

If that expression returns true, the loop executes, and we carry out the last instruction in the for clause (x++), adding 1 to x. The for loop then tests to see whether it should run again; if it does, then x++ runs again too. This continues until x <= returns false. 

The for loop is necessary for iterating any collections of data. Here is a typical example of the for loop:


main(List<String> arguments) {
  var proverb = StringBuffer('As Dark as a Dungeon.');
  for(var x = 0; x <= 10; x++){
    proverb.write("!");
    print(proverb);
  }
}


In the previous code, we used two built-in functions.

They are StringBuffer() and write(). We get these from Dart libraries.

The output is as follows:



As Dark as a Dungeon.!
As Dark as a Dungeon.!!
As Dark as a Dungeon.!!!
As Dark as a Dungeon.!!!!
As Dark as a Dungeon.!!!!!
As Dark as a Dungeon.!!!!!!
As Dark as a Dungeon.!!!!!!!
As Dark as a Dungeon.!!!!!!!!
As Dark as a Dungeon.!!!!!!!!!
As Dark as a Dungeon.!!!!!!!!!!
As Dark as a Dungeon.!!!!!!!!!!!

In our future discussions, we will use the for loop quite extensively, so currently, let’s stop here. 


You should understand the concept of why the exclamatory sign has increased from 0 to 10. 

It stops when a certain condition (here x=10) is met. 

I am now going to cover an interesting feature of iterating collections, namely, using Set and Map.

forEach() method

When the object you are going to iterate is Iterable, you can use the forEach() method. We are about to present two sets of collections; one is Set, and the other is Map.



main(List<String> arguments) {
  Set mySet = {1, 2, 3};
  var myProducts = {
    1 : 'TV',
    2 : 'Refrigerator',
    3 : mySet.lookup(2),
    4 : 'Tablet',
    5 : 'Computer'
  };
  var userCollection = {"name": "John Smith", 'Email': 'john@sanjib.site'};
  myProducts.forEach((x, y) => print("${x} : ${y}"));
  userCollection.forEach((k,v) => print('${k}: ${v}'));
}

As you see in the previous code, there are two sets, myProducts and userCollection. 

In both sets, a key=>value pair is declared. In the first case, 1 is key, and TV is the value. 

Now, Dart has a built-in forEach(key:value) method that can be used to give the output. 

In the first instance, x is the key, and y represents the value. After that, we use string interpolation to give the output.


Here is the output:


//output of code 2.5
1 : TV
2 : Refrigerator
3 : 2
4 : Tablet
5 : Computer
name: John Smith
Email: john@sanjib.site

Conclusion:

In this tutorial, we learnt how to implement looping in Dart using For and ForEach blocks and also tried a nested way to do it.

Hey I'm Venkat
Developer, Blogger, Thinker and Data scientist. nintyzeros [at] gmail.com I love the Data and Problem - An Indian Lives in US .If you have any question do reach me out via below social media