DEV Community

Cover image for For Loop in Dart
Jay Tillu
Jay Tillu

Posted on • Updated on

For Loop in Dart

  • for loop is a type of definite loop (it can also iterate for infinite times, but in practical cases, we use it when the number of iterations is known).

  • The for loop executes a block of code for a specified number of times.

The for loop is commonly used when the number of iterations is known.

Syntax


for (initialization; condition; step) {
  // Statements
}
Enter fullscreen mode Exit fullscreen mode

Sample Code


void main() {
  for (int i = 0; i < 3; i++) {
    print(i);
  }
}

Output
0
1
2
Enter fullscreen mode Exit fullscreen mode

How for Loop works

  • The initialization step is executed first and only once. This step allows us to declare and initialize any loop control.

  • In the next step, the condition is evaluated. If the condition is true then the body of for loop executes. If it is false then the flow of control jumps back to the next statement after for loop.

  • After the body of for loop executes, the flow of control jumps back to step, where the increment or decrement occurs. This statement allows us to update any loop control variables.

  • Then again condition is evaluated. If it is true then the whole process repeats again and again, until the condition becomes false.

  • Once the condition becomes false, the for loop is terminated and the flow of program goes to the next statement after the for a loop.

So, Guys, That’s it for the for loop. I hope you understand how for loop is structured and works in a dart. In other languages, just syntax changes but the core concept and working of loops will remain the same for any language. So Please spend some time with loops and understand how it works.

Till then Keep Loving, Keep Coding. And I’ll surely catch you up in next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn more about Dart and Flutter

Follow me for more such content

Top comments (0)