While
and do-while
loops in Dart, including:
-
🔄 Concept & Syntax
-
✅ Solved examples
-
❓ Practice problems (unsolved)
-
📌 Differences
-
🧾 Summary
🔁 while
and do-while
Loops in Dart – Explained with Examples
💡 Introduction
Loops are used when we want to repeat something again and again. In Dart, apart from for
, we also have while
and do-while
loops.
But what's the difference?
-
🧠 Use
while
when you don’t know how many times the loop should run. -
✅ Use
do-while
when you want the loop to run at least once, even if the condition is false.
🧱 1. while
Loop
🔁 Syntax:
while (condition) { // code to execute}
// code to execute}
🔍 How it works:
-
Checks the condition first.
-
If true, it runs the block.
-
Keeps repeating as long as the condition is true.
Checks the condition first.
If true, it runs the block.
Keeps repeating as long as the condition is true.
✅ Example 1: Print numbers from 1 to 5
void main() { int i = 1; while (i <= 5) { print(i); i++; }}
int i = 1; while (i <= 5) { print(i); i++; }}
Output:
1
2345
✅ Example 2: Find the sum of numbers from 1 to 10
void main() { int sum = 0, i = 1; while (i <= 10) { sum += i; i++; } print('Sum = $sum');}
int sum = 0, i = 1; while (i <= 10) { sum += i; i++; } print('Sum = $sum');}
🧱 2. do-while
Loop
🔁 Syntax:
do { // code to execute} while (condition);
// code to execute} while (condition);
🔍 How it works:
-
Executes the block first, then checks the condition.
-
Even if the condition is false, the loop runs once.
Executes the block first, then checks the condition.
Even if the condition is false, the loop runs once.
✅ Example 1: Print “Hello” once even if condition is false
void main() { int i = 10; do { print('Hello'); } while (i < 5);}
int i = 10; do { print('Hello'); } while (i < 5);}
Output:
Hello
✅ Example 2: Ask for password until correct (simulation)
import 'dart:io';void main() { String password = ''; do { stdout.write('Enter password: '); password = stdin.readLineSync()!; } while (password != 'admin123'); print('Access Granted!');}
void main() { String password = ''; do { stdout.write('Enter password: '); password = stdin.readLineSync()!; } while (password != 'admin123'); print('Access Granted!');}
🧠 break
and continue
in while
and do-while
🔴 Using break
to exit early
void main() { int i = 1; while (i <= 10) { if (i == 6) break; print(i); i++; }}
int i = 1; while (i <= 10) { if (i == 6) break; print(i); i++; }}
🟡 Using continue
to skip iteration
void main() { int i = 0; while (i < 5) { i++; if (i == 3) continue; print(i); }}
int i = 0; while (i < 5) { i++; if (i == 3) continue; print(i); }}
❓ Unsolved Practice Problems
-
Print all even numbers from 1 to 100 using a while
loop.
-
Count the number of digits in a number (e.g., 12345 → 5 digits).
-
Find the factorial of a number using do-while
.
-
Keep asking the user for input until they enter a number greater than 50.
-
Reverse a number using a while
loop (e.g., 123 → 321).
-
Use a do-while
loop to simulate a simple login system.
Print all even numbers from 1 to 100 using a while
loop.
Count the number of digits in a number (e.g., 12345 → 5 digits).
Find the factorial of a number using do-while
.
Keep asking the user for input until they enter a number greater than 50.
Reverse a number using a while
loop (e.g., 123 → 321).
Use a do-while
loop to simulate a simple login system.
🔍 Differences Between while
and do-while
Feature | while Loop |
do-while Loop |
---|---|---|
Condition check | Before loop body | After loop body |
Guaranteed execution | ❌ May not run at all | ✅ Runs at least once |
Use case | When unsure of execution | When one run is needed |
🧾 Summary
-
while
loop: Checks condition first.
-
do-while
loop: Runs code first, checks later.
-
Use break
to exit, continue
to skip.
-
Great for repeating input, validations, and menus.
while
loop: Checks condition first.
do-while
loop: Runs code first, checks later.
Use break
to exit, continue
to skip.
Great for repeating input, validations, and menus.