Programming in Dart: Control Flow & Collections

May 3 2022 · Dart 2.15, DartPad, DartPad

Part 1: Control Flow

02. Understand While Loops

Episode complete

Play next episode

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Introduction Next episode: 03. Challenge: Use While Loops
Transcript: 02. Understand While Loops

What if you had to write some Dart code to print out a string 100 times?

It would be tedious to write 100 print statements, but it would also violate the “Don’t Repeat Yourself” rule 99 times. That is, you don’t want lots of repeated code because instead of changing code in one place, you’d need to change in ninety other places.

Luckily, Dart has a tool to help with this: loops. In this episode, you’ll be trying out one particular kind of loop: the while loop.

Their syntax looks like this and they look a lot like an if statement!

The important difference is that as long as the condition of the while loops remains true, the code inside executes. So the condition is checked, if it’s true, the code executes, then the condition is checked again, and so on. You can see why it’s called a loop! The loop ends when the condition is checked and it turns out to be false.

You’ll start by writing a while loop. Then, you’ll create a variant of the while loop called the do while loop, and you’ll learn the differences between them. Let’s give it a shot!

To start, let’s create a while loop that will print out the numbers 1 through 9. You’ll need two things: the while loop itself, and a variable to track the number of times we’ve looped.

Start with the variable. Call it counter and set the value to 1:

var counter = 1;

Now you’re ready for the loop. Loop syntax starts with while, then a condition. A condition is simply a Boolean expression; you should recall Boolean expressions from the previous part of this course.

In this case, you want to print out the numbers from one to nine. So you want this loop to run up to nine, but no further. The simplest way is to say “while counter is less than ten”:

while (counter < 10)

You could also say “counter is less than or equal to 9”, which means the same thing.

Finally, you need to tell to print out the value of counter each time it goes around the loop. To do that, you add a set of curly braces to hold the code to execute:

while (counter < 10) {


And then inside those curly braces, you tell Dart to print(counter):

while (counter < 10) {
    counter += 1;

Once you’ve printed the value of counter, you need to increment the counter by one before the loop goes around again. Do you remember how to increment numbers? That’s right, with the plus equals operator:

while (counter < 10) {
  counter += 1;

Now when the code executes, you’ll see the numbers 1-9 appear in the console! Each time around the loop, Dart prints the current value of counter, then increments counter by one. When counter is ten, the condition here is no longer true, so Dart stops executing the loop.

Now, what would happen if you declared counter to be ten before the loop started? That’s right, it wouldn’t run. But what if you wanted to guarantee that a loop would execute at least once, before you check the condition for the loop?

You can use a variant of the while loop called the do while loop! First, put a print statement in there so you can tell in your console where you start counting down:

print('Counting up again');

And you also want to reset your counter to one, so do that after the “Counting up again” line:

counter = 1;

Remember, you’ve declared this as a variable above, so you don’t need to redeclare counter down here; you just need to assign it the value you want.

The syntax for a do-while loop is sort of backwards to the regular while loop: it starts with “do” followed by a set of curly braces with the loop code inside:

do {


So put the code you want to execute inside the curly braces; print the counter, then increment the counter by one:

do {
  counter += 1;

Now, what condition are you going to end with? You want to count up to nine, but remember that this loop will execute first, and then the condition will be checked.

You want this to print the numbers up to and including nine, and then stop. So your condition will be counter < 10, again. That means that when the counter is incremented to nine, the loop will run again, print nine, increment the counter to ten, and then the condition will be checked again, and the loop will stop.

do {
    counter += 1;
} while (counter < 10);

You can see in the console, that your code counts up to nine, and then counts up to nine again. Nice!~

To see the difference between these two loops, see what would happen if you set the counter to 10 before the loops started. Change other counter spot to ten.

var counter = 10;

counter = 10;

Now you can see the difference between the two loops. With the while loop, the condition is checked, but counter is not less than ten, so the code inside never executes. The do while loop, however, executes the first time and prints out “10”, and then checks the condition, finds the condition is false, and doesn’t repeat.

So the important difference is knowing when the condition is checked. A while loop checks the condition before the loop code, and do while checks the condition after.

There you go! Two different flavors of while loops in Dart!