For loop in C++

For loop

Here, in this section, we will discuss for loop in C++.

for loop

Looping in C++

Whenever, we need to do some repetitive task over and over again. We use looping, by using logical statements we tell our program how many times we want repetitive statements to be executed over and over.

Example –

  • Printing hello world 100 times
  • Printing first 100 natural numbers

C++ has the following loops to control the program execution –

A while and do-while loop have initialization and incrementation outside of their structure. A for loop accommodates the two inside the syntax itself.

Basic Syntax of a for Loop

for(statement1 ; statement2; statement3)
{
    // execute this code
}
  • Statement 1:  Called initialization part. Executed once, before execution of code block.
  • Statement 2: Called condition part of the loop. Executed every time before execution of code block to check if the condition is satisfied
  • Statement 3: Called incrementation part. Executing every time after code block inside is executed
For Loop in C++

Another way of writing the above could also be –

// initilization happens once before the first execution of code block
// condition checking happens everytime before code block can be executed
for(initialization ; condition(s); incrementation)
{
    // execute this code
    
    // incrementation happens everytime after code block is executed
}
Let us look at a few examples to understand more about how for loops work –

Example 1

Printing hello world 5 times
Run
#include<iostream>
using namespace std;
 
int main()
{
    for(int i = 1; i <= 5; i++){
        cout << "Hello World, i: " << i << endl;
    }
    return 0;
}
 

Output

 
Hello World, i: 1
Hello World, i: 2
Hello World, i: 3
Hello World, i: 4
Hello World, i: 5

Example 2

Printing first N Natural Numbers
Run
#include<iostream>
using namespace std;
 
int main()
{
    int n = 10;
    
    for(int i = 1; i <= n; i++){
        cout << i << " ";
    }
    return 0;
}
 

Output

 
1 2 3 4 5 6 7 8 9 10

Example 3

Printing first even numbers up to 20
Run
#include<iostream>
using namespace std;
 
int main()
{
    int n = 20;
    
    for(int i = 0; i <= n; i = i + 2){
        cout << i << " ";
    }
    return 0;
}
 

Output

 
0 2 4 6 8 10 12 14 16 18 20

Example 4

Print all items of a character array
Run
#include<iostream>
using namespace std;
 
int main()
{
    char arr[] = {'H', 'e', 'l', 'l', 'o'};
    
    // array starts at index 0
    for(int i = 0; i < 5 ; i++){
        cout << arr[i] << " ";
    }
    return 0;
}
 

Output

 
H e l l o

Example 5

Print all items of a character array
Run
#include<iostream>
using namespace std;
 
int main()
{
    string s1 = "PrepInsta";
    
    // string ends when you encounter \0 character at the end
    // if this new do not worry its a little too advanced
    // we will learn this in strings part of C++
    for(int i = 0; s1[i] != '\0' ; i++){
        cout << s1[i] << " ";
    }
    return 0;
}
 

Output

 
P r e p I n s t a

Infinite For Loop

A for loop may run infinitely if the wrong conditions are given –

Example 1

Run
#include<iostream>
using namespace std;
 
int main()
{
    int i = 1;
    
    // we can keep all 3 parts : initialization, condition, increment
    // empty and the for loop will still run
    // but will run infinitely
    for( ; ; ){
        cout << i << " ";
    }
    return 0;
}
 

Output

 
1 1 1 1 1 1 ... printed infinitely ...
Similarly the following will also run infinitely –
// Example 2:
// Missing inc/dec part
// i will remain 0 and i < 10 condition will always be true
for(int i = 0; i < 10;) // Example 3: // Forcibly putting true value in condition for(int i = 0; 1 ; i++) // Example 4: // Always true, because 0 is less than i: 10 and we incrementing i value for(int i = 10; i > 0; i++)

Inappropriate use of the semicolon(;)

A semicolon would force the for loop to get stuck and not go to next line. An example is below –

Example

int i;
for( i = 0; i < 6; i++); // valid but wrong OP cout << i; // only 6 prints 1 time}

Ranged Based for Loop

After C++11, we got a new range-based for loop was this works with collections such as arrays and vectors. Its syntax is:
for (variable : collection) {
    // statement(s)
}

Example

Run
#include<iostream>
using namespace std;
 
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    for(int n: arr){
        cout << n << " ";
    }
    return 0;
}
 

Output

 
1 2 3 4 5 6 7 8 9 10