For loops in Dart

🔁 Mastering for Loops in Dart – A Complete Beginner’s Guide


💡 Introduction

When you need to repeat a task multiple times, instead of writing the same code again and again, you use a loop. Among the loops available in Dart (for, while, do-while, and for-in), the for loop is the most common and powerful.

Think of a for loop like a teacher taking attendance: she starts at roll number 1 and continues until all students are checked.


📋 Keywords

  • for

  • break

  • continue

  • i++ (increment)

  • i-- (decrement)


🧠 Understanding the for Loop

🔁 Syntax:

for (initialization; condition; increment/decrement) {
// body of loop
}

🧱 Parts Explained:

  • Initialization: Runs once at the beginning (e.g., int i = 0)

  • Condition: Checked before every loop run (e.g., i < 5)

  • Increment/Decrement: Runs after each loop iteration (i++, i--, etc.)


✅ Basic Example:

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

Output:

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5

🔧 Variations of for Loop


1. Countdown Loop

void main() {
for (int i = 5; i >= 1; i--) {
print('Countdown: $i');
}
}

2. Loop with Step Size (e.g., print even numbers)

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

3. Loop with break

Stops the loop early.

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

Output:

1 2 3 4 5

4. Loop with continue

Skips current iteration.

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

Output:

1 2 4 5

5. Nested for Loops (Pattern Printing)

void main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
stdout.write('*');
}
print('');
}
}

Output:

*
**
***

✅ Solved Examples


1. Sum of first 10 numbers

void main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
print('Sum = $sum');
}

2. Multiplication Table of 7

void main() {
for (int i = 1; i <= 10; i++) {
print('7 x $i = ${7 * i}');
}
}

3. Reverse String

void main() {
String text = 'dart';
String reverse = '';
for (int i = text.length - 1; i >= 0; i--) {
reverse += text[i];
}
print('Reversed: $reverse');
}

❓ Unsolved Practice Problems

You can try solving these using a for loop in DartPad or your local IDE.


1. Print numbers from 1 to 50


2. Print all odd numbers between 1 to 100


3. Print squares of numbers from 1 to 10


4. Print a triangle pattern of # like:

#
##
###
####

5. Calculate factorial of a number (e.g., 5! = 120)


6. Find the sum of all even numbers from 1 to 100


7. Count digits in a given number (e.g., 51293 → 5 digits)


🧾 Summary

Feature Description
for loop Repeats code using counter logic
break Stops loop early
continue Skips current round, goes to next
i++, i-- Common ways to step through the loop
Nested loops Used for grids or patterns

🔚 Conclusion

The for loop is your best friend when repeating tasks in Dart. Whether it’s printing a series, summing numbers, or building patterns, for is fast, clean, and easy to use.


Would you like a downloadable PDF with diagrams, MCQs, and fill-in-the-blanks for your students? Or should I format this as an HTML blog post with colors and cards?


Contact us for software training, education or development










 

Post a Comment

Me