Welcome to the thrilling realm of iterations and loops. In this lesson, let's dive into Dart's while
and do while
loops! While
loops play a vital role in programming due to their ability to execute code blocks repeatedly until a specific condition becomes false. They operate similarly to performing an action until a condition changes — such as washing a car until it's completely clean. A while
loop operates until the condition becomes false. Conversely, a do while
loop will execute the code block at least once, regardless of the condition.
A while
loop in Dart consists of a condition and a code block. The code block is executed if the given condition is true.
Dart1while (condition) { // Loop continues while the condition is true 2 // Block of code 3}
while
loops in Dart are control flow statements that enable a specific code block to execute repeatedly while a certain condition holds true. For example, imagine a scenario where you continue reading a book while the light is on. Here's what a while
loop in Dart might look like:
Dart1int counter = 0; // Initialize with 0 2while (counter < 5) { // Loop while counter is less than 5 3 print(counter); // Print the counter 4 counter++; // Increment the counter by 1 after each loop iteration 5} 6/* 7Prints: 80 91 102 113 124 13*/
The do while
loop in Dart is similar to the while
loop, but with one crucial difference: the code block within the do while
loop will execute at least once, even if the condition is not met.
Here's the general syntax for a do while
loop in Dart:
Dart1do { 2 // Code block to be executed 3} while (condition);
And here's a simple practical example:
Dart1int counter = 5; 2do { 3 print(counter); 4 counter++; 5} while (counter < 5); 6/* 7Prints: 85 9*/
In this example, despite the counter being initialized at 5
which does not satisfy the condition counter < 5
, the code block is still executed once. Hence, 5
is printed out.
Suppose you need to execute a countdown from the number 5 to 0. You can quickly achieve this using a while
loop as follows:
Dart1int number = 5; // Initialize with 5 2 3while (number >= 0) { // While number is at least 0 4 print('Number is: $number'); // Print the number 5 number--; // Decrease the number by one after each iteration 6} 7/* 8Prints: 9Number is: 5 10Number is: 4 11Number is: 3 12Number is: 2 13Number is: 1 14Number is: 0 15*/
while
loops in Dart can use compound conditions with &&
(and) or ||
(or). For instance, imagine a situation where you're saving up for a new device and aim to stop saving once you reach your target, or when the sales season ends — whichever occurs first:
Dart1int savings = 0; // Starting savings 2bool salesSeason = true; // Identifying the start of sales 3 4while (savings < 200 && salesSeason) { // Continue while savings is less than 200 and it's a sales season 5 savings += 20; // Increase savings by 20 6 print('Savings: $savings'); 7 8 // Simulate the ending of sales season 9 if (savings == 100) { // When savings equals 100, sales season ends 10 salesSeason = false; 11 } 12} 13/* 14Prints: 15Savings: 20 16Savings: 40 17Savings: 60 18Savings: 80 19Savings: 100 20*/
Be careful to avoid creating an endless loop in Dart, where the condition always remains true, leading to an infinite loop. This situation can occur when the increment step is missing, as shown in the example below:
Dart1int counter = 0; // Initial counter value 2 3while(counter < 5) { // Counter is always less than 5 4 print(counter); 5 // counter++; 6 // Counter isn't increasing (commented), thus creating an infinite loop 7}
Congratulations! You've mastered the use of while
loops, grasped their syntax, and implemented them in real-world scenarios. Now it's time to delve deeper with some practical exercises to strengthen these skills. Enjoy your journey into the Dart world, and prepare for exciting adventures ahead!