The for Loop in C++ :

for loop:

C language for Loop - The for loop is the easiest to understand of the C++ loops. All its loop control elements are gathered in one place, while in the other loop construction of C++, they are scattered about the program. The general syntax of the for loop is:

for(initialization expression;test expression;update expression)

Example :

  1. /* Description: Print 1 to 10 use for loop in C++ language */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int i;
  7. for(i=1;i<=10;i++)
  8. {
  9. cout << "\n"<<i <<"\n";
  10. }
  11. }

Output :

C++ language Print 1 to 10 use for loop

Note : If you change i<=10 to i=<20 inside the for loop, than this loop print 1 to 20.

Nested Loops :

A loop may contain another loop in his body This form of a loop is called nested loop. But in a nested loop, the inner loop must terminate before the outer loop.

  1. /* Description: simple nested loop program in C++ language */
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int i,j;
  7. for(i=1;i<=20;i++)
  8. {
  9. cout << "\n";
  10. for(j=1;j<=i;j++)
  11. {
  12. cout <<"*";
  13. }
  14. }
  15. }

Output :

C++ language simple nested loop program

Next Update


Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels