Are you prepared to dive into the deep end of Dart? Today, we embark on an exciting journey focused on mastering two fundamental instructions: the break
and continue
statements. In its most basic terms, the break
statement ceases a loop from executing, even if its original condition still holds true. However, the continue
statement merely skips the remaining portion of the current iteration, instantly moving on to the next one.
We'll familiarize ourselves with these statements within the context of simple for
and while
loops before diving into the dynamics of nested loops.
Do you remember what the for
loop does? Its iterations persist as long as the condition is met. In this section, we delve into understanding the break
statement. Upon encountering this statement, the loop immediately terminates — no questions asked!
Consider this scenario: a game of hide-and-seek, where each hiding spot represents an iteration of the loop. The game ends when the hiding object (break
) is found. Here's how it could be presented using Dart:
Dart1for (var i = 0; i <= 5; i++) { 2 if (i == 3) { 3 print('Hidden object found at position $i'); // Our hidden object is at position 3 4 break; // STOP! We found the object. No need to search further. 5 } 6 print('No hidden object at position: $i'); 7} 8 9// Output: 10// No hidden object at position: 0 11// No hidden object at position: 1 12// No hidden object at position: 2 13// Hidden object found at position 3
Notice that the number series from 0
to 5
couldn't fully execute due to the break
at i = 3
, which prematurely halted the remaining iterations.
Next, we have the continue
statement. This statement, in contrast, opts to skip the current iteration and directly progress to the next. It's akin to choosing candies from a jar but skipping one specific candy.
Dart1for (var i = 0; i <= 5; i++) { 2 if (i == 3) { // Position 3 has the candy we're avoiding 3 continue; // SKIP! Don't take this candy. On to the next one! 4 } 5 print('Picked candy at position: $i'); 6} 7 8// Output: 9// Picked candy at position: 0 10// Picked candy at position: 1 11// Picked candy at position: 2 12// Picked candy at position: 4 13// Picked candy at position: 5
Here, the continue
statement effectively excludes i = 3
, and the loop covers all values from 0
to 5
.
The break
and continue
statements function similarly in while
loops just as they do in for
loops. Let's assume you're scrolling through a music playlist, but you decide to skip track number 2:
Dart1var track = 0; 2 3while (track < 5) { 4 track++; 5 if (track == 2) { // We are at track number 2 6 continue; // SKIP! Let's move on to the next track. 7 } 8 print('Playing track number: $track'); 9} 10 11// Output: 12// Playing track number: 1 13// Playing track number: 3 14// Playing track number: 4 15// Playing track number: 5
Now, consider a scenario where we're flipping through a photo album and wish to stop when we reach a specific photo:
Dart1var photo = 0; 2 3while (photo < 100) { 4 photo++; 5 if (photo == 23) { // Found our preferred photo 6 print('Found the photo at position: $photo'); 7 break; // STOP! We've found the photo. No need to keep flipping. 8 } 9} 10 11// Output: Found the photo at position: 23
Consider a situation in a school where we are tasked with finding a specific student named "Alex" across multiple classrooms. Each classroom is likened to a layer in our search operation, with each student representing a point of investigation within those layers. The moment "Alex" is found within any classroom, we intend to cease our search in that particular layer. This is a perfect scenario to illustrate the power of the break
statement, which allows for an immediate halt in the search operation:
Dart1for (var classroom = 0; classroom < 2; classroom++) { 2 for (var student = 0; student < 5; student++) { 3 // Assuming each classroom holds 5 students for simplicity 4 if (student == 2) { // Alex is found at position 3 in any given classroom 5 print('Found Alex in classroom $classroom'); 6 break; // Alex is found. No need to further inspect this classroom. 7 } 8 print('Searching in classroom $classroom, student $student'); 9 } 10} 11 12// Output: 13// Searching in classroom 0, student 0 14// Searching in classroom 0, student 1 15// Found Alex in classroom 0 16// Searching in classroom 1, student 0 17// Searching in classroom 1, student 1 18// Found Alex in classroom 1
This example demonstrates the break
statement's critical role: it halts the inner loop (student search within a classroom) when Alex is found, without halting the search in other classrooms (the outer loop). This concise illustration shows how break
can effectively manage control flow within nested loops, ensuring efficient searches and operations.
Good job! You've successfully navigated through the break
and continue
statements in Dart. You've learned how to control single and nested for
and while
loops using these statements. Keep practicing what you've learned today, and stick around for the next lesson!