Friday 27 February 2015

while loop in C++

while loop in C++    Loops are basically use in C++ and other programing language for repetition. Loop repeats the statemen... thumbnail 1 summary

while 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
int a=1;                    
while (a<10) 
{
cout<<"Pakistan"<<endl; 
a=a+1;                                     
};

The only different with for loop and while loop is that for loop in memory efficient and the while is not memory efficient t because the variable use in the for loop is deleted from the memory when the loop is executed but in the while loop the variable use in the loop is still remain in the memory when the loop is executed.

There are three main parts in while  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()
{
int a=1; 
while(a<10)
{
cout<<”Pakistan “<<endl;
};
a++;
system(“pause”);
return 0;
};
Output :
Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan Pakistan

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

Example 3
#include<iostream.h>
Int main()
{
int a=0; 
while(a<10)
{
cout<<a<<endl;
};
a=a+2;
system(“pause”);
return 0;
};
Output : 0 2 4 6 8
Example 4
#include<iostream.h>
Int main()
{
int a=1; 
while(a<10)
{
cout<<a<<endl;
};
a++;
system(“pause”);
return 0;
};

output: 0 3 6 9

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