Table of contents
1.
Introduction
2.
For loops 
3.
While loops
4.
For Each loops
5.
For In loops
6.
Do While loops
7.
Frequently Asked Questions
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Dart Loops

Author Vasu Bansal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Loops are used to perform a set of operations that need to be repeated continuously for a given number of times. These are one of the fundamental operations that one needs to learn while learning any new programming language.

New to Dart? Don't worry. We got you all covered. Feel free to navigate to the blog Dart SDK Installation and Environment Setup on the Coding Ninjas Website to set up the Dart SDK on your system if you have not already done so.

For loops 

For loops in Dart are similar to those in Java and C. They are used to perform an operation a fixed number of times. The following is the syntax of the for loop in Dart.

for(initialization_exp; condition_check; test_expression){
    // Write all the operations/statements here that need to be looped
}

The flow of the code goes as follows. First of all, the initialization expression is executed, and this expression is executed only once for a loop. After that, the control moves to the condition check expression. If the condition check expression satisfies, then we move to the loop's body. The loop's body contains all the code whose execution needs to be repeated. After executing the body, the control flow moves towards the test expression, where some operation is performed on the looping variable. Again the code moves towards the condition check expression, and if the condition satisfies, it enters the loop, and the cycle continues. The breaking out point from the loop comes when the condition check expression fails. In such a scenario, the execution flow jumps out of the loop and goes to the line written just after the loop. The following is a sample program to demonstrate the use of for loops in Dart.

Code:

void main() {
  for (int i = 0; i < 5; i++) {
    print('Hello Ninja number ${i + 1}');
  }
}

Output:

Hello Ninja number 1
Hello Ninja number 2
Hello Ninja number 3
Hello Ninja number 4
Hello Ninja number 5

You must have guessed the control flow by now. First of all, we executed the initialization statement of the loop where we declare a looping variable named and initialize its value to zero. After that, we check the condition, i < 5. Currently, we have the value of i as zero, this condition satisfies, and we enter the body of the loop, where we encounter a simple print statement. The print statement prints the following line on the terminal.

Hello Ninja number 1

After that, we move the expression i++, where we increment the value of i from 0 to 1 and again perform the condition check. Again i < 5 (1 <5), so we enter the body of the loop and execute the print statement. This continues until the statement i++ makes the value of equal to 5. When the variable i takes the value 5, the condition check i < 5 would fail, and we will exit the loop.

While loops

The operation performed by while loops are again the same as for loops. The only difference lies in their syntax. The syntax of while loops goes as follows.

while(condition_exp)
{
// body of the loop
}

The loop's body keeps on executing until the condition expression goes on evaluating to true. The following is a sample program to demonstrate the use of while loops in Dart.

Code:

void main() {
  int i = 0;
  while(i < 5) {
    print('Hello Ninja number ${i + 1}');
     i++;
  }
}

Output:

Hello Ninja number 1
Hello Ninja number 2
Hello Ninja number 3
Hello Ninja number 4
Hello Ninja number 5

For Each loops

For-each loops are used to iterate over an array/list of elements. For each element of the list, the for-each loop passes it to a function where we can perform any subsequent operation. The following is a sample program to demonstrate the use of for-each loops in Dart.

Code:

void main() {
  List<String> subjects = ["DSA", "OS", "DBMS", "CN"];
  subjects.forEach((var subject)=> print("I love ${subject}"));
}

Output:

I love DSA
I love OS  
I love DBMS
I love CN

For In loops

The concept of For-in is similar to those in Java or Python. It directly picks the reference of the object in the list. The following is a sample program to demonstrate the use of for-in loops in Dart.

Code:

void main() {
  List<String> subjects = ["DSA", "OS", "DBMS", "CN"];
  for(String subject in subjects){
    print("I love ${subject}");
  }
}

Output:

I love DSA
I love OS  
I love DBMS
I love CN

Must Read What are Loops in Java.

Do While loops

These are quite similar to the while loops that we discussed earlier. The only difference lies in the flow control of the code execution. In the case of do-while loops, the loop's body is executed first, and after that, the subsequent condition check expression is evaluated. Recall, in the case of while loops, the condition is checked first, and then the body of the loop is executed. The syntax of the do-while loops is as follows.

do{
// body of the loop
} while(condition_check);

The following is a sample program to demonstrate the use of do-while loops in Dart.

Code:

void main() {
  int i = 0;
  do{
    i++;
    print("Welcome Ninja no ${i}");
  }while(i < 7);
}

Output:

Welcome Ninja no 1
Welcome Ninja no 2
Welcome Ninja no 3
Welcome Ninja no 4
Welcome Ninja no 5
Welcome Ninja no 6
Welcome Ninja no 7

Frequently Asked Questions

  1. Is there any other way to simulate loops in Dart?
    Yes, you can simulate loops with the help of labels in Dart. We can use own our labels to create a looping action in our program.
     
  2. What is the use of the break statement?
    The break statement is used to exit a loop based on some custom condition check inside your body of the loop. The break statements cannot be used outside a loop.
     
  3. When should I use the continue statement?
    The continue statement is used to skip the subsequent lines of code in the body of the loop and jump back to the start of the loop. The continue statements cannot be used outside a loop.

Conclusion

In this article, we have extensively discussed the different kinds of loops in Dart and saw sample programs depicting the implementation of each of those types of loops. You can also read the blog Dart Classes and Objects on the Coding Ninjas Website to learn about Classes and Objects in the Dart programming language.

We hope that this blog has helped you enhance your knowledge regarding Dart Loops. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass