...

Introduction to For Loops

Every loop you've written so far has had three parts:

  1. Initialization: creating a counter variable (e.g. let i = 1)
  2. Condition: checking whether to keep looping (e.g. i <= 5)
  3. Update: changing the counter after each iteration (e.g. i++)

A for loop puts all three parts on a single line, separated by semicolons:

for (initialization; condition; update)

This makes loops more compact and easier to read at a glance. The code sample shows a for loop that counts from 1 to 5. Run it step by step to see how it works.

Variables

No variables

Loading...

Output (Console)