Loops
These are also called as Repetitive control structure.Sometime We want a program or a part of program to repeat again and again then we use Loops.This type of program execution is called as looping.There Are three types of loops These are also called as
- While Loop
- Do-While Loop
- For Loop
While Loop:
Structure of While loop.
while(condition)
{
statement;
}
As from Above structure It is clear that how it work.First we shall Apply a condition.If condition is true then it repeats itself until the condition become false.Here is the example of while loop.I hope It shall Help You very much
Program
/*
In This program We shall find Table of the given number by the user with the help of while loop
*/
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int x=1,y=1,z=1;
cout<<"Enter A number to find table ";
cin>>x;
while(z<=10)
{
cout<<x<<" * "<<y<<" = "<<x*y<<"\n";
z++; // Every time z increase by one
y++;v // Every time y increase by one
}
getch();
}
OuTput
DO-While Loop :
In Do-while loop the whole program repeats itself again and again until it's condition become false..
STRUCTURE
do
{
statement;
}while(condition);
Note : That the loop body is always executed at least once. One important difference between the while loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution the loop body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop termination test is Performed after each execution of the loop body. hence, the loop body is always executed least once.
Program
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int x=0,y=0,z=0;
do
{
cout<<"Enter Number ";
cin>>x;
cout<<"Enter 2nd NUmber ";
cin>>y;
cout<<"The sum Of the numbers is = "<<x+y<<"\n";
cout<<"Do U want to repeat The Program Enter '1' to repeat ";
cin>>z;
}while(z==1);
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int x=0,y=0,z=0;
do
{
cout<<"Enter Number ";
cin>>x;
cout<<"Enter 2nd NUmber ";
cin>>y;
cout<<"The sum Of the numbers is = "<<x+y<<"\n";
cout<<"Do U want to repeat The Program Enter '1' to repeat ";
cin>>z;
}while(z==1);
getch();
}
OUTPUT
You can Repeat it even more than 100000000000000+ times if every time u want to repeat it..
FOR LOOP
Structure of for loop
for (initialization; decision; increment/decrement)
{
statement(s);
}In a for loop there are 3 values needed
- Initialization of loop control variable
- Testing of loop control variable
- Update the loop control variable either by incrementing or decrementing.
Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is true, the program executes the body of the loop and then the value of loop control variable is updated. Again it checks the condition and so on. If the condition is false, it gets out of the loop.






No comments:
Post a Comment