How To Use For Loop Using C++ language in DEV C++

How To Use For Loop Using C++ language in DEV C++

I will discuss two things here
  • For Loop
  • Nested For loop
  • Exercises

For Loop

For loop is just like a counting process. For example if you want to execute same statement ten times , in that case you will go for writing the same statement ten times. It is correct but a good programmer does n't go for it because he/she does not want to make his/her program lengthy. So here for loops help them.


Syntax

for( initializing ; boolean expression or condition ; increment or decrement )
{
      statements ;
}


In syntax, first step is to initialize the variable which may be integer type or other depends upon your program. Second step is to write a condition which means that how many times this loop will execute the statements inside it. Third step is to increment or decrement means after execution the statements every time it is incremented or decremented the variable by one upto condition you apply in the second step shows that loop is executed just like counting.

Example:
let i want to count from 0 to 100 using for loop. my program looks like this





In this program i am using endl which means go to next line nothing else.Go to execute option in the menu bar of DEV C++ , click on compile and run or use short key F11. .exe file will appear like this , shows the output




you can see that text Number from 0 to 100 is not executing again and again , because statement for it is not written inside the for loop. Check out program code.

Nested For loop

Nested for loops are easy to under stand , if you can know the behavior of for loop.

Syntax

for( initializing ; boolean expression or condition ; increment or decrement)
{

     for( initializing ; boolean expression or condition ; increment or decrement)
    {
     .
     .
     .

    }
     statements;
}


 Dots represent that you can use any number of for loops.

Example:
Lets take an example on it . Carefully look at the program , you can easily understand the program behavior.
I will define two variables i and j. For the first cycle of first loop, 2nd loop will execute five times according to values of i and j defined in the program.This will continue till the final cycle of first loop appears.

Program code:



Output:  after compiling and executing , the output will be





Hope that you will find this helpful.

Exercises:
1.What is output of following program ?

#include<iostream>
using namespace std;


int main()
{
int x = 1;
cout<< " Numbers  "<< endl<<endl<<endl;

for(  int i=x ; i<=100 ; i++  )
{

cout<<  i << "  ";

}

cout<<endl<<endl<<endl;

}

2.What is output of following program ?

#include<iostream>
using namespace std;


int main()
{
int x = 1;
cout<< " Numbers  "<< endl<<endl<<endl;

for(  int i= 10; i<=100 ; i++  )
{

cout<<  i << "  ";

}

cout<<endl<<endl<<endl;

}

3.What is output of following program ?

#include<iostream>
using namespace std;


int main()
{
int x = 10;
cout<< " Numbers  "<< endl<<endl<<endl;

for(  int i= 10; i<=100 ; i++  )
{

cout<<  i << "  ";

}

cout<<endl<<endl<<endl;

}

4.What is output of following program ?

#include<iostream>
using namespace std;


int main()
{
int x = 10;
cout<< " Numbers  "<< endl<<endl<<endl;

for(  int i= 10; i<=100 ; i++  )
{

cout<<  i << "  ";

}

cout<<endl<<endl<<endl;

}
5.What is output of following program ?

#include<iostream>
using namespace std;


int main()
{
int x = 10;
cout<< " Numbers  "<< endl<<endl<<endl;

for(  int i= 10; i<=10 ; i++  )
{

cout<<  i << "  ";

}

cout<<endl<<endl<<endl;

}

6.What is output of following program ?

#include<iostream>
using namespace std;


int main()
{
int x = 0 ,y = 10;
cout<< " Numbers  "<< endl<<endl<<endl;

for(  int i= 10; i<=10 ; i++  )
{

cout<<  i << "  ";

}
cout<<endl<< y ;
cout<<endl<<endl<<endl;

}
7.What is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 3 ,y = 0;
for(int i=x;i<5;i++)
{
cout<<"value of i = "<< i <<endl<<endl;
    for(int j=y;j<5;j++)
{
cout<<"values of j  = "<< j <<endl;

}
cout<<endl;
}

getch();

}

8.What is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 3 ,y = 0;
for(int i=0;i<5;i++)
{
cout<<"value of i = "<< i <<endl<<endl;
    for(int j=y;j<5;j++)
{
cout<<"values of j  = "<< j <<endl;

}
cout<<endl;
}

getch();

}

9.What is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 3 ,y = 3;
for(int i=0;i<5;i++)
{
cout<<"value of i = "<< i <<endl<<endl;
    for(int j=y;j<5;j++)
{
cout<<"values of j  = "<< j <<endl;

}
cout<<endl;
}

getch();

}

10.What is output of following program ?

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int x = 3 ,y = 3;
for(int i=x;i<5;i++)
{
cout<<"value of i = "<< i <<endl<<endl;
    for(int j=y;j<5;j++)
{
cout<<"values of j  = "<< j <<endl;

}
cout<<endl;
}

getch();

}

3 comments:

  1. Nyc program but can you do it in dev c only not in dev c++

    ReplyDelete
  2. Anonymous10:33 pm

    Write programs in devc++

    ReplyDelete
  3. Anonymous5:30 pm

    #include
    using namespace std;


    int main()
    {
    int x = 1;
    cout<< " Numbers "<< endl<<endl<<endl;

    for( int i=x ; i<=100 ; i++ )
    {

    cout<< i << " ";

    }

    cout<<endl<<endl<<endl;

    }

    ReplyDelete

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.