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: writefor,while, anddo whileloops, control their flow withbreakandcontinue, 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
forloop; - understand initialization, condition, and increment;
- use a
whileloop; - use a
do whileloop; - 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, anddo 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