# For Loop in Dart

### For Loop

This is the most common type of loop. You can use for loop to run a code block multiple times according to the condition. The syntax of for loop is:

``````for(initialization; condition; increment/decrement){
statements;
}
``````
• Initialization is executed (one time) before the execution of the code block.
• Condition defines the condition for executing the code block.
• Increment/Decrement is executed (every time) after the code block has been executed.

### Example 1: To Print 1 To 10 Using For Loop

This example prints 1 to 10 using for loop. Here int i = 1; is initialization, i<=10 is condition and i++ is increment/decrement.

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

Show Output

### Example 2: To Print 10 To 1 Using For Loop

This example prints 10 to 1 using for loop. Here int i = 10; is initialization, i>=1 is condition and `i--` is increment/decrement.

``````void main() {
for (int i = 10; i >= 1; i--) {
print(i);
}
}
``````

Show Output

### Example 3: Print Name 10 Times Using For Loop

This example prints the name 10 times using for loop. Based on the condition, the body of the loop executes 10 times.

``````void main() {
for (int i = 0; i < 10; i++) {
print("John Doe");
}
}
``````

Show Output

### Example 4: Display Sum of n Natural Numbers Using For Loop

Here, the value of the total is 0 initially. Then, the for loop is iterated from i = 1 to 100. In each iteration, i is added to the total, and the value of i is increased by 1. Result is 1+2+3+….+99+100.

``````void main(){

int total = 0;
int n = 100; // change as per required

for(int i=1; i<=n; i++){
total = total + i;
}

print("Total is \$total");

}
``````

Show Output

### Example 5: Display Even Numbers Between 50 to 100 Using For Loop

This program will print even numbers between 50 to 100 using for loop.

``````void main(){
for(int i=50; i<=100; i++){
if(i%2 == 0){
print(i);
}
}
}
``````

Show Output

### Infinite Loop In Dart

If the condition never becomes false in looping, it is called an infinite loop. It uses more resources on your computer. The task is done repeatedly until the memory runs out.

This program prints 1 to infinite because the condition is i>=1, which is always true with i++.

``````void main() {
for (int i = 1; i >= 1; i++) {
print(i);
}
}
``````

### Video

Watch our video on for loop in Dart.