Wednesday, 4 March 2015

For loop in C++ Programming

 How to use For loop in C++ for repition For loop in C++ For loop in C++ Loops are basically use in C++ and other programing language... thumbnail 1 summary

 How to use For loop in C++ for repition

For loop in C++


For loop in C++ Loops are basically use in C++ and other programing language for repetition. Loop repeats the statement or a group of statement again and again until its condition is become wrong then the execution will stop. For example if we want to display the world Pakistan on the screen five time so we will use to write the word Pakistan five time using cout statement like this
 
cout<<"Pakistan";
cout<<"Pakistan"; 
cout<<"Pakistan";
cout<<"Pakistan";
cout<<"Pakistan";




 


But to display the word Pakistan using the loop so we use maximum of three line even if we want to display the world Pakistan 100 time or 10000 time in C++ program.
Syntax
 
for(staring point ; condition; increment or decrement)
statement or a group of statements
There are three main parts for loop. 1 Starting point Starting point is use to tell the compiler that from where the loop starts. For example start loop form 2 or 0 or 1 basically an integer or character is used for Starting point. For example int a=1; maybe a starting point or char a=’A’; be a starting point. 2 Condition Condition is use to tell the compiler that where the loop ends. For example end loop when 10 or 20 value reaches basically an integer or character is used for ending condition. For example
 
a<20;
a>30;
 

3 Increment or Decrement It is used to increment to increment or decrement the variable used in the loop basically in for loop it is increment by 1 on each time of execution. But we can incremented it by 2 times or 3 time or what ever we want For Example
 
a++;    
0r
a=a+1;


This will increment the variable by adding 1 to its original value on each time of exaction. We can use the increment operator to increase the value of variable by adding 3 to its original value. a=a+3; This will increment the variable by adding 3 to its original value on each time of exaction.
 Example 1
 
#include<iostream.h>
Int main()
{
for(int a=1; a<10;a++)
cout<<”Pakistan “<<endl;
system(“pause”);
return 0;
};
Example 2
 
#include<iostream.h>
int main()
{
for(int a=5; a<10;a=a+1)
cout<<”Pakistan “<<endl;
system(“pause”);
return 0;
};
Output Pakistan Pakistan Pakistan Pakistan Pakistan 
Example 3
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a++)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 0,1,2,3,4,5,6,7,8,9 
Example 4
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a=a+2)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 0,2,4,6,8 
Example 5
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a=a+3)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 0,3,,6,9
 Example 6
 
#include<iostream.h>
Int main()
{
for(int a=0; a<10;a=a+2)
cout<<”masterofcplusplus<<endl;
system(“pause”);
return 0;
};
Output Masterofcplusplus Masterofcplusplus Masterofcplusplus Masterofcplusplus Masterofcplusplus 

Example 7
 
#include<iostream.h>
Int main()
{
for(int a=3; a<10;a++)
cout<<a<<endl;
system(“pause”);
return 0;
};
Output 3,4,5,6,7,8,9