A for loop is used when we want to repeat the same action several times. Instead of writing many similar lines of code, we write the action once and tell C++ how the repetition should work.
For example, if we want to print numbers from 1 to 10, we should not write ten separate output lines. We can use one loop:
This prints: 12345678910
The main idea is simple: the loop has a variable, usually called i, and this variable changes after every repetition. We use to know which step of the loop we are currently doing.
i
Core concept: How for works
The general form of a for loop is:
There are four important parts:
<start> prepares the loop before it begins.
<condition> is checked before every repetition.
<update> changes the loop variable after every repetition.
<body> is the code that we want to repeat.
Let us look at the same example again:
Here is what each part means:
<start> is i=1. We create the variable i and start it from 1.
<condition> is i≤10. The loop continues while this condition is true.
<update> is i=i+1. After each repetition, i increases by 1.
<body> is the output command. It prints the current value of i.
The loop works in this order:
To clearly see each part, it helps to place the for loop header on several lines:
Now we will trace the loop completely. For every iteration, each part of the loop gets its own step.
The four parts of a `for` loop, fully traced
C++
source · C++line 2
1for (
2 int i = 1; // <start>
3 i <= 10; // <condition>
4 i = i + 1 // <update>
5) {
6 cout << i << ' '; // <body>
7}
Run <start>
L2
The <start> part runs once before the loop begins.
inv
The order is always: <start>, <condition>, <body>, <update>, then <condition> again.
A for loop can also go backwards. If we want to print numbers from 10 down to 1, we can start from 10 and decrease i:
This prints: 10987654321
So a for loop is not only for counting upward. We choose the start value, the condition, and the update depending on the task.
Examples
Example 1: Print numbers from 1 to n
Given one integer n, print all numbers from 1 to n.
Example:
Approach and solution
We need to print numbers in increasing order. The first number is 1, the last number is n, and each next number is larger by 1.
So the loop should start with i=1, continue while i≤n, and update with i=i+1.
Algorithm:
Implementation:
Example 2: Print odd numbers from 1 to n
Given one integer n, print all odd numbers from 1 to n.
Example:
Approach and solution
Odd numbers are 1,3,5,7,…. Every next odd number is larger by 2.
So we start from 1, continue while i≤n, and update with i=i+2.
Algorithm:
Implementation:
Example 3: Sum numbers from 1 to n
Given one integer n, find the sum of all numbers from 1 to n.
Example:
Because 1+2+3+4+5=15.
Approach and solution
This time we do not print every number. We keep a variable sum, which stores the total answer so far.
At the beginning, sum=0. Then the loop goes through all values of i from 1 to n. On each step, we add the current i to sum.
Algorithm:
Implementation:
We use longlong for sum because the answer can become large when n is large.
Example 4: Print a number several times
Given two integers x and k, print the number x exactly k times.
Example:
Approach and solution
Here the loop variable is only used for counting repetitions. It is not the value we print.
We run the loop from 1 to k. On each step, we print x.
Algorithm:
Implementation:
This example is important because it shows that the loop variable does not always have to be printed. Sometimes it only controls how many times the loop runs.
Example 5: Countdown
Given one integer n, print numbers from n down to 1.
Example:
Approach and solution
Now the numbers go in decreasing order. The loop should start from n, continue while i≥1, and update with i=i−1.
Algorithm:
Implementation:
for(int i = 1; i <= 10; i = i + 1){ cout << i << ' ';}
1. Run start once.
2. Check the condition.
3. If the condition is false, stop the loop.
4. If the condition is true, run the body.
5. Run the update.
6. Go back to step 2.
i = 1 -> condition is true -> print 1
i = 2 -> condition is true -> print 2
i = 3 -> condition is true -> print 3
...
i = 10 -> condition is true -> print 10
i = 11 -> condition is false -> stop
i = 1 -> print 1
i = 3 -> print 3
i = 5 -> print 5
i = 7 -> print 7
i = 9 -> print 9
i = 11 -> condition is false, stop
Input:
5
Output:
1 2 3 4 5
1. Read n.
2. For i from 1 to n:
1. Print i.
Input:
10
Output:
1 3 5 7 9
1. Read n.
2. For i from 1 to n, jumping by 2:
1. Print i.
Input:
5
Output:
15
1. Read n.
2. Set sum = 0.
3. For i from 1 to n:
1. Add i to sum.
4. Print sum.
Input:
7 4
Output:
7 7 7 7
1. Read x and k.
2. For cnt from 1 to k:
1. Print x.
Input:
5
Output:
5 4 3 2 1
1. Read n.
2. For i from n down to 1:
1. Print i.
for(<start>;<condition>;<update>){<body>}
for(int i = 1; i <= 10; i = i + 1){ cout << i << ' ';}
for( int i = 1; // <start> i <= 10; // <condition> i = i + 1 // <update>){ cout << i << ' '; // <body>}
for(int i = 1; i <= 10; i = i + 2){ cout << i << ' ';}
for(int i = 10; i >= 1; i = i - 1){ cout << i << ' ';}
#include <bits/stdc++.h>using namespace std;int main(){ int n; cin >> n; for(int i = 1; i <= n; i = i + 1){ cout << i << ' ';} return 0;}
#include <bits/stdc++.h>using namespace std;int main(){ int n; cin >> n; for(int i = 1; i <= n; i = i + 2){ cout << i << ' ';} return 0;}
#include <bits/stdc++.h>using namespace std;int main(){ int n; cin >> n; long long sum = 0; for(int i = 1; i <= n; i = i + 1){ sum = sum + i;} cout << sum << endl; return 0;}