JavaScript AcademyProfessional Engineering

Learning object 12 of 100

While-loop visualizer

Step through while, do...while and loop-control programs and watch the condition and every variable change on each pass.

Loading…

What it shows

A while loop checks its condition before every pass; do...while checks after, so its body always runs once. Each pass must change something the condition depends on. break leaves the loop at once and continue jumps to the next check. The algorithm program shows an accumulator, a counter and a maximum updating together.

Guided experiment

  1. In Savings goal, change the deposit and predict the number of months before stepping.
  2. In Skip and stop, follow where execution goes after continue and after break.
  3. In Loop patterns, change one value to a negative number. Does the maximum still work?

Predict

let n = 5; do { n++; } while (n < 3); What is n afterwards?

Related

Lessons: Lesson 17: While and do-while loops, Lesson 18: Break and continue, Lesson 20: Solving problems with iteration

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-012.js in the companion repository.