Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - FOR LOOP


The for loop in C++ is like the while loop. But the for loop is way more powerful than the while loop.


The for loop has inbuilt features, by which you can reduce lines of code.


So far we have seen, how while loop was used to System.Console.WriteLine "Hello World" 5 times.


Example :



#include <iostream>
using namespace std;

int main() {

    int x = 1;
    while (x <= 5) {
        cout << "Hello World" << endl;
        x = x + 1;
    }
    
    return 0;
}



The same program could be reduced to less lines of code using the for loop.


Let us see below.


Example of for loop:


Let us use for loop to print "Hello World" five times on the screen.


Example :



#include <iostream>
using namespace std;

int main() {

    for(int x = 1; x <= 5 ; x++) 
    {   
  		cout << "Hello World" << endl;
	}  
    
    return 0;
}


Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World

Now, if we break the contents of for loop, we get :

  1. Initialisation part : It is run before the loop starts.
    java_Collections


    x is getting initialized with value 1 (i.e. int x = 1).

  2. Conditional part : It states the condition for running the loop.
    java_Collections


    It says, continue to be in the loop until the value of x is not more than 5.

  3. Increment part : It is run each time after the loop.
    java_Collections


    x++ is the Increment part. i.e. Same as x = x + 1.

All together forms the for loop.


for(int x = 1; x <= 5 ; x++) {
	cout << "Hello World" << endl;
}

So in the 1st iteration,


1st Iteration


The control gets into the for loop and checks, if the value of x is less than or equal to 5.

java_Collections
java_Collections

In this case the value of x is 1. Which is less than 5.


So, the control enters the for loop.


for(int x = 1; x <= 5 ; x++) {
	cout << "Hello World" << endl;
}

and finds the System.Console.WriteLine statement,


cout << "Hello World" << endl;

So, it prints Hello World.

Output :



  Hello World

Next, C++ finds, it has come to the end of the loop.

java_Collections

So, it increments the value of x by 1 (And C++ does the increment internally).


And the value of x becomes 2.

java_Collections

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5(i.e. Internally C++ checks if i <= 5).

java_Collections

In this case the value of x is 2. Which is less than 5.


So, the control enters the for loop.

java_Collections

And finds the print statement,


cout << "Hello World";

So, it prints Hello World for the second time.

Output :



  Hello World
  Hello World

Similarly, C++ finds, it has come to the end of the loop.

java_Collections

And increments the value of x by 1.

java_Collections

Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.


3rd Iteration

Output :



  Hello World
  Hello World
  Hello World
java_Collections

4th Iteration

Output :



  Hello World
  Hello World
  Hello World
  Hello World
java_Collections

5th Iteration

Output :



  Hello World
  Hello World
  Hello World
  Hello World
  Hello World
java_Collections

Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

java_Collections

In this case, the value of x is 6. Which more than 5.


And the control comes out of the for loop.


Now, if you see the output, Hello World is exactly printed 5 times.


So, we have seen, how to print Hello World 5 times.


Now, let us see, how can we print the numbers from 1 to 5.


Printing the numbers 1 to 5 using 'for' loop


Example :



#include <iostream>
using namespace std;

int main() {

	for(int x = 1; x <= 5 ; x++) {   
  		cout << x << endl;
	}
	
	return 0; 
}	


Output :



  1
  2
  3
  4
  5

The above code is self explanatory.


However, we have provided the explanation below.



CLICK HERE TO SEE THE EXPLANATION.


So in the 1st iteration,


1st Iteration


The control gets into the for loop and checks, if the value of x is less than or equal to 5.

java_Collections
java_Collections

In this case the value of x is 1. Which is less than 5.


So, the control enters the for loop.


for(int x = 1; x <= 5 ; x++) {
	cout << x << endl;
}

and finds the print statement,


cout << x;

So, it prints the value of x, which is currently 1.

Output :



  1

Next, C++ finds, it has come to the end of the loop.

java_Collections

So, it increments the value of x by 1 (And C++ does the increment internally).


And the value of x becomes 2.

java_Collections

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

java_Collections

In this case the value of x is 2. Which is less than 5.


So, the control enters the for loop.

java_Collections

And finds the System.Console.WriteLine statement,


cout << x;

So, it prints the value of x, which is 2 currently.

Output :



  1
  2

Similarly, C++ finds, it has come to the end of the loop.

java_Collections

And increments the value of x by 1.

java_Collections

Similarly, the third, fourth and fifth Iteration continues incrementing the value of x, printing it.


3rd Iteration

Output :



  1
  2
  3
java_Collections

4th Iteration

Output :



  1
  2
  3
  4
java_Collections

5th Iteration

Output :



  1
  2
  3
  4
  5
java_Collections

Once again, the control comes to the for loop and checks, if the value of x is less than or equal to 5.

java_Collections

In this case, the value of x is 6. Which more than 5.


And the control comes out of the for loop.


Example of 'for' 'each' loop:


We will be learning about it in the Arrays section of C++.