Loops in Dart

Loops 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)

·

6 min read

There are many situations that occur while coding when we want to repeat a certain block of code a number of times. Now there are two ways to do the job.

  1. Write that block of the code number of times.

  2. Use Loops.

Now in the first case, the problem is your code will become too lengthy and boilerplate. And also after a certain time, it will become too hard to maintain that code. And as there is a general rule in programming that “never repeat your code”. So to rescue you in that situation loops come into the picture. To repeat some statements a number of times we use loops. The statements inside the body of the loop will keep repeating until the condition becomes false. The repetition of a loop is called Iteration in programming.

There are two types of loops are there in programming:

  1. Definite

  2. Indefinite

Definite Loop — The Loop whose number of iterations is fixed, where you know the exact number of iterations before entering the loop, then it is called a definite loop. Ex: For Loop.

Indefinite Loop — The Loop whose number of iterations is not fixed, where you don’t know the exact number of iterations before runtime, then it is called an indefinite loop. Ex: While Loop & Do-While Loop

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
}

Sample Code

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

Output
0
1
2

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 the program goes to the next statement after the for a loop.

While Loop in Dart

  • While loop is a type of indefinite loop.

  • The while loop executes the block of code until the condition is true.

  • We mostly use a while loop when we don’t know how many times the loop will actually execute at runtime.

  • In simple terms when iterations of a loop are known then we mainly use for loop and when iterations are unknown then we use while loop.

Syntax

while (condition) {
  //Statements to be executed if the condition is true
}

Program

void main() {
  int number = 0;

  while (number < 10) {
    if (number % 2 == 0) {
      print(number);
    }
    number++;
  }
}

Output
0
2
4
6
8

Do-While Loop in Dart

  • do-while loop is a type of indefinite loop.

  • do-while loop first executes the statements and then checks for the condition. If the condition is true it will keep executing and if the condition is false loop will terminate.

  • So whether the condition is true or false, do while loop’s statements will execute at least once.

Syntax

do {
  Statement(s) to be executed;  
} while (expression);
  • Here also take a look that in for loop or while loop we don’t use semicolon at the end of the loop. But in the do-while loop semicolon is mandatory after the while statement.

  • Incrementer or decrementer must be placed inside the do’s block, it cannot be placed outside the do’s block.

Program

void main() {
  int number = 0;
  do {
    if (number % 2 == 0) {
      print(number);
    }
      number++;
  } while (number < 10);
}

Output
0
2
4
6
8

Conclusion

In conclusion, loops are essential constructs in programming that allow us to efficiently repeat a block of code multiple times. They come in two main types: definite loops and indefinite loops.

Definite loops, exemplified by the for loop in Dart, are used when the number of iterations is known beforehand. The for loop consists of an initialization step, a condition that determines whether to continue looping and an increment or decrement step to update the loop control variable. It is commonly employed when the exact number of iterations is fixed and known.

Indefinite loops, on the other hand, do not have a fixed number of iterations and are used when the loop's termination depends on a runtime condition. The while loop in Dart is a typical example of an indefinite loop. It executes the code block as long as the condition remains true, and we use it when we are unsure about the number of iterations required.

The do-while loop is another form of an indefinite loop. It is similar to the while loop, but it guarantees that the loop's body will execute at least once, as the condition is checked after the first iteration. Unlike for and while loops, the do-while loop requires a semicolon after the while statement and mandates that the incrementer or decrementer be placed within the loop's block.

In general, loops offer a concise and maintainable way to handle repetitive tasks in code. They help us follow the fundamental principle of programming: "Don't repeat yourself," as we can efficiently manage repetitive operations without duplicating code. Choosing the appropriate type of loop based on the nature of the iteration is crucial in optimizing code and ensuring its readability and maintainability.

So, guys, That’s all you need to know about loops. Please let me know if I miss something. I’ll be happy to learn from you. Till Then Keep Loving, Keep Coding. I’ll surely catch you up in the next article. Jai Hind, Vande Mataram 🇮🇳

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.

💡
Subscribe To My Newsletter For More Such Content.

Learn More about Dart and Flutter

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!