0%
Loops: for, while, do while — Chapter 05

Chapter 05 — Loops: for, while, do while

Part 1A — Learn Dart · Chapter 05 of 62 · 4 h

Chapter 05 — Loops: for, while, do while

Level: beginner Estimated time: 4 h Prerequisites: Chapter 04 — Conditions (if, else, switch) What you will be able to do by the end: write for, while, and do while loops, control their flow with break and continue, and nest loops to traverse 2D grids.


05.0 — Chapter objectives

By the end of this chapter, you will be able to:

  • understand why loops are necessary;
  • repeat an instruction several times;
  • use a for loop;
  • understand initialization, condition, and increment;
  • use a while loop;
  • use a do while loop;
  • use break;
  • use continue;
  • create counters;
  • iterate over ascending or descending values;
  • use a loop with conditions;
  • create nested loops;
  • avoid infinite loops;
  • choose between for, while, and do while;
  • prepare for working with lists in the next chapter.

05.1 — Why do we need loops?

Suppose we want to display:

Niveau 1
Niveau 2
Niveau 3
Niveau 4
Niveau 5

We could write:

void main() {
  print('Niveau 1');
  print('Niveau 2');
  print('Niveau 3');
  print('Niveau 4');
  print('Niveau 5');
}

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!