Coding/Learn/cpp/for loops
C++ Foundational Course
01.Why C++ Powers the Modern World
02.Anatomy of a C++ Program
03.Semicolons, Statements & Comments
04.Variables & Fundamental Data Types
05.Console I/O: std::cout and std::cin
06.Arithmetic, Modulo & Increment Operators
07.Branching Decisions: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements: Fast Multi-Way Selection
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Functions: Modular, Reusable Code Blocks
14.Pass by Value vs Pass by Reference (&)
15.Fixed-Size Arrays & Contiguous Memory
16.Dynamic Arrays: std::vector and STL
17.Strings Deep Dive & Manipulation
18.Unmasking Memory: Pointers & Addresses (& and *)
19.Custom Data Types: Structs & OOP Basics
20.Capstone: Command-Line Bank & Account System
Course Certificate0%

Complete all lessons to earn

LearnThe Standard For Loop & Counting
11 / 20 0%
Lesson 11 of 20
5 mins read

The Standard For Loop & Counting

PreviousNext Lesson
In Plain English

When you know in advance exactly how many times a block should run (e.g. 10 times, or over every element in an array), the for loop bundles initialization, condition, and increment into a single compact header line.

The Mental Model

"A for loop is like a digital lap counter at a track meet: (Start at Lap 1; Run until Lap 10; Add 1 lap each time you cross the finish line)."

Deep Dive: How It Works

Header Structure: for (initialization; condition; increment)

1. Initialization: Runs ONCE at the start (e.g. int i = 0;).

2. Condition: Evaluated before each iteration. If true, body runs. If false, loop terminates.

3. Increment: Executes after the body finishes each loop (e.g. i++ or i += 2).

Scoped Variable: The loop index i is local to the for loop and disappears when the loop finishes.

Syntax Blueprint

for (int i = 0; i < 5; i++) {
  std::cout << i << " ";
}

Initializes i to 0, loops while i < 5, adds 1 after each cycle (prints 0 1 2 3 4).

PointCore Rules to Remember

Point star
Zero-Indexed Counting: Programmers almost always start loops from 0 (for (int i = 0; i < N; i++)).
Point star
Compact & Clean: Keeps all loop control logic in one visible line, preventing forgotten counter bugs.
Point star
Custom Steps: You can increment by any step (e.g. i += 5) or count down (i--).

Common Beginner Traps & How to Fix Them

Mistake:Using commas instead of semicolons in the for header: for(int i=0, i<5, i++)

Why it happens: Typo confusion.

How to fix: The three clauses in for(...) must be separated by semicolons (;).

Pro Tip: In C++11 and beyond, you can also use range-based for loops: for (int x : myVector) { }!

Live Interactive Example

Hit Run Code to see it live
Counting and Accumulating with For Loops
C++ 20
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: The for loop runs 5 times, accumulating the sum from 1 to 5.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Print even numbers 2 through 8

  • PointWrite a for loop starting at int i = 2, running while i <= 8, stepping with i += 2.
  • PointPrint each number followed by a space.
1
2
3
4
5
6
7
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates