While and Do While in Dart

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
}

🔍 How it works:

  • 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++;
}
}

Output:

1
2
3
4
5

✅ 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');
}

🧱 2. do-while Loop

🔁 Syntax:

do {
// 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.


✅ Example 1: Print “Hello” once even if condition is false

void main() {
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!');
}

🧠 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++;
}
}

🟡 Using continue to skip iteration

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

❓ Unsolved Practice Problems

  1. Print all even numbers from 1 to 100 using a while loop.

  2. Count the number of digits in a number (e.g., 12345 → 5 digits).

  3. Find the factorial of a number using do-while.

  4. Keep asking the user for input until they enter a number greater than 50.

  5. Reverse a number using a while loop (e.g., 123 → 321).

  6. 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.



Contact us for software training, education or development










 

Post a Comment

Me
Me
WhatsApp Contact Me on WhatsApp
×

📩 Today’s Programming Tip

✅ Python Tip: Use dictionary comprehensions for quick dict creation.
Example:
squares = {x: x*x for x in range(5)}
print(squares)

🔗 Learn More

💡 Tip of the Day