While and Do-While Loops in Dart
Loops are fundamental programming constructs that allow you to execute a block of code repeatedly based on certain conditions. In Dart, the while
and do-while
loops provide mechanisms for iteration, enabling you to perform actions until a specified condition is met. Understanding these loops is essential for creating dynamic and responsive applications.
While Loop
Overview
The while
loop repeatedly executes a block of code as long as a specified condition evaluates to true. The condition is checked before each iteration, so if it's false at the outset, the loop's body may never execute.
Syntax
while (condition) {
// Code to execute while condition is true }
Example of a While Loop
Here’s a simple example that prints numbers from 1 to 5 using a while
loop.
Code:
void main() {
int i = 1; // Initialization
while (i <= 5) {
print(i); // Print the current value of i i++; // Increment i }
}
Explanation:
int i = 1;
: Initializesi
to 1.while (i <= 5) {
: Checks ifi
is less than or equal to 5.- Inside the loop,
print(i);
outputs the current value ofi
, andi++
incrementsi
by 1. - The loop continues until
i
exceeds 5.
Do-While Loop
Overview
The do-while
loop is similar to the while
loop, but it guarantees that the loop body will execute at least once, regardless of the condition. This is because the condition is evaluated after the loop's body has executed.
Syntax
do {
// Code to execute } while (condition);
Example of a Do-While Loop
Here’s an example that demonstrates a do-while
loop to print values from 1 to 5.
Code:
void main() {
int i = 1; // Initialization
do {
print(i); // Print the current value of i i++; // Increment i } while (i <= 5);
}
Explanation:
int i = 1;
: Initializesi
to 1.- The
do
block executes first, printing the current value ofi
. - After executing the loop body, the condition
while (i <= 5)
is checked. - If true, the loop continues; otherwise, it stops.
Key Differences Between While and Do-While Loops
- Condition Evaluation:
- While Loop: The condition is checked before the loop body executes. If the condition is false initially, the loop may not run at all.
- Do-While Loop: The loop body executes at least once before the condition is checked.
- Use Cases:
- While Loop: Use when the number of iterations is not known in advance, and you want to check the condition before each iteration.
- Do-While Loop: Use when you need the loop body to execute at least once, such as when prompting a user for input.
Example Programs
Example 1: Sum of Natural Numbers Using While Loop
This program calculates the sum of natural numbers up to a specified limit using a while
loop.
Code:
void main() {
int n = 10; // Change this value for a different range int sum = 0;
int i = 1; // Initialization
while (i <= n) {
sum += i; // Add the current number to sum i++; // Increment i }
print('The sum of the first $n natural numbers is: $sum');
}
Example 2: User Input Validation Using Do-While Loop
This program prompts the user to enter a number until they provide a valid positive integer, demonstrating the use of a do-while
loop.
Code:
import 'dart:io';
void main() {
int number;
do {
print('Please enter a positive number:');
number = int.parse(stdin.readLineSync()!);
} while (number <= 0); // Keep prompting until a positive number is entered
print('You entered: $number');
}
Example 3: Countdown Timer Using While Loop
This program implements a simple countdown timer that counts down from a specified number to zero.
Code:
void main() {
int countdown = 5; // Starting point
while (countdown > 0) {
print(countdown);
countdown--; // Decrement countdown }
print('Countdown finished!');
}
Conclusion
The while
and do-while
loops are essential constructs in Dart for repetitive tasks and iteration. The choice between them depends on whether you need the loop body to execute at least once. Understanding these loops and their applications will enhance your ability to control the flow of your Dart programs effectively.
PLAY QUIZ