Programming Fundamentals

Loops

12 min · 2 worked examples · 3 questionsFree

Repeating work without repeating yourself, and how to avoid getting stuck forever.

After this lesson you can

  • Write a for loop with the right number of passes
  • Choose between for and while
  • Avoid a loop that never ends

Why loops exist

To beep three times you could write the same four lines three times over. To beep a hundred times that becomes absurd, and if you later change the pitch you must change it in a hundred places.

A loop repeats a block of code, and everything you might want to change lives in exactly one place. That is not just less typing, it is what makes code maintainable.

In the full lesson

  1. Why loops exist
  2. The for loop
  3. The while loop
  4. Nested loops

Free with an account. Read it, work the examples, answer the questions, then build it in the simulator.