Lesson 13 / الدرس 13

The for loop / حلقة for

The same three parts as a while loop, gathered onto one line so they cannot be forgotten. This is the loop you will write most, and the one whose shape is worth knowing by heart.

الأجزاء الثلاثة نفسها من حلقة while، مجموعة في سطر واحد فلا تُنسى. وهذه هي الحلقة التي ستكتبها أكثر، والتي يستحق شكلها الحفظ.

for (let i = 0; i < 5; i++) {
      └───┬───┘  └──┬─┘  └┬┘
          │         │     └ 3. after every round
          │         └────── 2. checked before every round
          └──────────────── 1. once, at the start
  console.log(i);
}

This prints 0 1 2 3 4 — five rounds, starting at zero.
The three parts of lesson 12 are all present and all on one line, which is why the for loop is harder to write an infinite version of by accident. Run the demonstration to see the order the three parts actually execute in.

Why it starts at zero

Almost every for loop you will see starts at 0 rather than 1, and it is not arbitrary: lists in JavaScript are numbered from 0, as lesson 15 will show. Starting the counter at 0 means i lines up with the positions in a list, and i < length is exactly the right condition. Fighting that convention makes every loop over a list slightly wrong.

The shapes you will write

To do thisWrite
Five rounds, 0 to 4for (let i = 0; i < 5; i++)
Five rounds, 1 to 5for (let i = 1; i <= 5; i++)
Count downfor (let i = 5; i > 0; i--)
Every second onefor (let i = 0; i < 10; i += 2)
Through a listfor (const item of items)

The last row is a different loop and usually the better one for a list: it gives you each item directly, with no counter to get wrong. Lesson 15 uses it constantly. Reach for a counter only when you genuinely need the position.

Loops nest, and a nested loop runs its whole inner loop for every single round of the outer one. Two loops of ten is a hundred rounds; two of a thousand is a million. That is fine for a multiplication table and worth thinking about the moment the numbers come from real data — it is the difference between a page that responds and one that does not.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. In for (let i = 0; i < 5; i++), when does i++ run?

2. Why do for loops usually start at 0?

3. Two nested loops of 1000 rounds each. How many times does the inner body run?

Your task / مهمتك

Print a multiplication table from 1 to 9 as a real HTML table, using two nested loops with counters named for what they count. Then use a single loop with % to print only the numbers between 1 and 100 that are divisible by both 3 and 5. Finally, count how many times the inner body of your nested loop ran, and print that number.

اطبع جدول ضرب من 1 إلى 9 جدولَ HTML حقيقيًا، بحلقتين معششتين وعدّادين مسمّيين بما يعدّان. ثم استخدم حلقة واحدة مع % لطباعة الأرقام بين 1 و100 القابلة للقسمة على 3 و5 معًا. وأخيرًا عُدّ كم مرة عمل المتن الداخلي لحلقتك المعششة، واطبع ذلك الرقم.

  • A real table, built by two nested loops جدول حقيقي مبني بحلقتين معششتين
  • Counters named for what they count عدّادان مسمّيان بما يعدّان
  • The divisible-by-both list, using % قائمة القابل للقسمة على الاثنين، بـ%
  • The inner-body count printed عدد تنفيذ المتن الداخلي مطبوع
How do you want to submit? / كيف تريد التسليم؟