Through the use of some examples, we will learn about the C++ for loop and how it functions in this tutorial.
Computer programmers employ loops to continually execute a block of code.
Let’s say, for illustration, that we want to display a message 100 times. Then we can use a loop rather than writing the print statement 100 times.
That was just a simple illustration; by using loops effectively, we can make our programs much more sophisticated and efficient.
C++ offers three different kinds of loops.
- for loop
- while loop
- do…while loop.
This guide’s main emphasis is on the C++ for loop. In the upcoming tutorials, we will learn about the additional types of loops.
C++ for loop
The syntax of for-loop is:
for (initialization; condition; update) { // body of-loop }
Here,
- initialization: variable initialization; only once execution.
- condition: If true, the for loop’s body is run. The for loop ends if the condition is false.
- update: changes the initialised variables’ values, then rechecks the condition.
Example 1: Printing Numbers From 1 to 5
#include <iostream> //geektocode.com using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << i << " "; } return 0; }
OUTPUT:
1 2 3 4 5
The following describes how this programme operates:
Iteration | Variable | i <= 5 | Action |
---|---|---|---|
1st | i=1 | true | 1 is printed. i is increased to 2. |
2nd | i=2 | true | 2 is printed. i is increased to 3. |
3rd | i=3 | true | 3 is printed. i is increased to 4. |
4th | i=4 | true | 4 is printed. i is increased to 5. |
5th | i=5 | true | 5 is printed. i is increased to 6. |
6th | i=6 | false | The loop is terminated. |
Example 2: Display a text 10 times
// C++ Program to display a text 10 times #include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; ++i) { cout << "GeektoCode! " << endl; } return 0; }
OUTPUT:
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
GeektCode!
Example 3: Find the sum of first n Natural Numbers
// C++ program to find the sum of first n natural numbers //geektocode.com #include <iostream> using namespace std; int main() { int num, sum; sum = 0; cout << "Enter a positive integer: "; cin >> num; for (int i = 1; i <= num; ++i) { sum += i; } cout << "Sum = " << sum << endl; return 0; }
OUTPUT:
Enter a positive integer: 10
Sum = 55
Ranged Based for Loop
A new range-based for loop was added in C++11 to support working with collections like arrays and vectors. The syntax is:
for (variable : collection) { // body of loop }
In this case, the for loop is executed for each item in the collection, and the value is then assigned to the variable.
Example 1: Range Based for Loop
#include <iostream> using namespace std; int main() { int num_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int n : num_array) { cout << n << " "; } return 0; }
OUTPUT:
1 2 3 4 5 6 7 8 9 10
An int array named the num array has been declared and initialized in the program above. Ten items make up it.
Here, a range-based for loop is being utilized to access every element of the array.
C++ Infinite for loop
// infinite for loop for(int i = 1; i > 0; i++) { // block of code }
Because the condition in the programme above is always true, the code will run indefinitely.
We will learn about the while and do…while loop in the upcoming tutorial.