for Loop (Counter-Controlled Iteration)
📌 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).
🚀 Practice in ArkyEdu Labs & Related Guides
🎮 Launch Interactive Lab →
Bucle cu Contor for, do-while & Aplicații Interdisciplinare STEM
📖 Full Lesson & Curriculum →
Clasa a VII-a · Bucle for/do & Fizică C++
🏫 Grade 7 Hub →
All modules & guides for this grade
🏫 Grade 8 Hub →
All modules & guides for this grade
📚 Top 10 C++ Errors Fixed →
Troubleshoot the 10 most common C++ mistakes in Grades 7–8 (Code::Blocks): = vs ==, stray semicolons after if/while, infinite loops, 5/2 integer division, and n = 0.
❓ 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)while vs. do-while Loops in C++ →
Both while and do-while repeat code as long as a condition holds true. A while loop checks the condition first (may run 0 times), whereas a do-while loop checks at the end (always runs at least once).
Array (1D Vector) in C++ →
A 1D array is an indexed collection of elements sharing the same data type stored contiguously in memory under a single name. In C++, native array indices start at 0.
Non-Blocking Arduino Timing: millis() vs. delay() →
The delay(ms) function freezes the entire Arduino processor during the pause, missing button presses and urgent sensors. The millis() function returns milliseconds since boot, letting multiple LEDs, buzzers, and sensors run simultaneously without blocking.
Algorithm & Core Algorithm Properties →
An algorithm is a finite, unambiguous, step-by-step set of instructions that transforms input data into the desired output within a finite number of steps.