Programming in Dart: Control Flow & Collections

May 3 2022 · Dart 2.15, DartPad, DartPad

Part 1: Control Flow

03. Challenge: Use While Loops

Episode complete

Play next episode

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

It’s time for your next challenge! Your challenge is to create a loop that counts down from ten and instead of zero, you should print out the words, “Blast Off!”. That’s right, we’re launching rockets from raywenderlich.com - pause the video and now and try it out.

How’d that challenge go for you. Hopefully, it wasn’t too bad. A little resistance will go a long way to make you a better developer. Lets start at the top. Open up a fresh DartPad. Start by creating a new counter variable. Set it to 10.

var counter = 10;

Now comes our while loop. This loop will run so long as the counter is greater than zero.

while (counter > 0) {

}

Next print out the counter then make sure to decrement it.

print(counter);
counter -= 1;

Finally after the loop, print out the text ‘Blast Off!’.

print('Blast Off!');

And that’s it - you have a countdown. Now run the app. We get a countdown followed by the words, Blast Off!.