🚀
ArkyEdu GLOSAR TIC
RO Practice Lab
Algorithms, C++ & Scratch Grades 7–8 RO Term: Bucla for (Structura Repetitivă cu Contor)

for Loop (Counter-Controlled Iteration)

💬 Share on WhatsApp
📌 Quick Definition (Answer-First)

A for loop executes a block of code a known number of times by packing the counter initialization, loop condition, and increment step into one compact header.

How It Works & Why It Matters

It is the go-to loop for iterating from 1 to n, finding divisors, generating sequences, and scanning arrays.

Its three header parts are always separated by semicolons (;).

💻 Standard C++ for Loop
int sum = 0;
for (int i = 1; i <= n; i++) {
  sum += i;
}

🏫 Classroom Example

Looping backwards: for (int i = 10; i >= 1; i--) cout << i << " ";

⚖️ for vs. while

Choose for when the number of iterations or index range is known; choose while when stopping depends on dynamic input or digit reduction.

⚠️ Common Mistake in Exams & Homework

Using commas instead of semicolons in the header, or writing i < n instead of i <= n when n should be included (off-by-one error).

❓ Quick Questions & Answers

What is an off-by-one error in a for loop?
Stopping one iteration too early or too late, such as writing i < n instead of i <= n when counting from 1 through n.

Related Glossary Terms

← Back to Full Glossary (A–Z)