Labs ICT
Pro Login

For Loop

The for Loop

So you need to repeat something a known number of times. The for loop is your tool. It bundles initialization, condition, and increment into one tidy line.

for (int i = 0; i < 5; i++) {
  cout << i << "\n";
}

The initialization runs once at the start. The condition is checked before each iteration — if false, the loop stops. The increment runs after each iteration.

Any of the three parts can be empty, but the semicolons stay.

int sum = 0;
for (int n = 1; n <= 100; n++) {
  sum += n;
}
cout << sum;
Try it Yourself →