For Loop in Dart

The for loop in Dart is a control flow statement that allows you to execute a block of code repeatedly for a specified number of iterations. It is particularly useful when you know in advance how many times you want to execute a statement or a block of statements. A for loop consists of an initialization section, a condition, and an increment or decrement expression. This structure allows for concise and efficient iteration through collections, arrays, or sequences of numbers.

Syntax

The basic syntax of a for loop in Dart is as follows:

for (initialization; condition; increment/decrement) {
  // Code to execute in each iteration }

Components:

  • Initialization: This is executed once at the beginning and is typically used to declare and initialize loop control variables.
  • Condition: This is evaluated before each iteration. The loop continues as long as this condition is true.
  • Increment/Decrement: This is executed at the end of each iteration and is used to update the loop control variable.

Simple Example

Here's a simple example that uses a for loop to print numbers from 1 to 5.

Code:

void main() {
  for (int i = 1; i <= 5; i++) {
    print(i);
  }
}

Explanation:

  • for (int i = 1; i <= 5; i++): This initializes i to 1, checks if i is less than or equal to 5, and increments i by 1 after each iteration.
  • print(i);: This prints the current value of i in each iteration.

Example Programs

Example 1: Sum of First N Natural Numbers

This program calculates the sum of the first n natural numbers using a for loop.

Code:

void main() {
  int n = 10; // Change this value for a different range   int sum = 0;

  for (int i = 1; i <= n; i++) {
    sum += i; // Add the current number to the sum   }

  print('The sum of the first $n natural numbers is: $sum');
}

Explanation:

  • int n = 10;: Sets the upper limit for the sum.
  • int sum = 0;: Initializes the sum variable.
  • The loop iterates from 1 to n, adding each value to sum.
  • After the loop, it prints the total sum.

Example 2: Multiplication Table

This program generates a multiplication table for a given number.

Code:

void main() {
  int number = 5; // Change this value for a different number 
  for (int i = 1; i <= 10; i++) {
    print('$number x $i = ${number * i}');
  }
}

Explanation:

  • int number = 5;: Sets the number for which the multiplication table will be generated.
  • The loop runs from 1 to 10, multiplying number by i and printing the result in each iteration.

Example 3: Reverse a String

This program reverses a given string using a for loop.

Code:

void main() {
  String original = "Hello, Dart!";
  String reversed = '';

  for (int i = original.length - 1; i >= 0; i--) {
    reversed += original[i]; // Concatenate characters in reverse order   }

  print('Original: $original');
  print('Reversed: $reversed');
}

Explanation:

  • String original = "Hello, Dart!";: The string to be reversed.
  • String reversed = '';: Initializes an empty string to hold the reversed version.
  • The loop starts from the last character of original and decrements i until it reaches the first character, concatenating each character to reversed.
  • Finally, it prints both the original and reversed strings.

Conclusion

The for loop in Dart is a powerful tool for performing repeated actions efficiently. It allows you to iterate over sequences, perform calculations, and manipulate data easily. Understanding how to use for loops effectively is essential for developing robust Dart applications.

PLAY QUIZ

What are the three main components of a for loop in Dart?

Initialization, Condition, Termination

Initialization, Condition, Increment/Decrement

Start, Check, Step

Begin, Validate, Update