Professional Documents
Culture Documents
Lab # 10
Lab # 10
10
IMPLEMENTATION OF ITERATIVE STATEMENT IN C++
Objectives:
Introduction:
Loop: (Fig 1)
Theory:
1 while loop
Repeats a statement or group of statements while a given condition is true. It tests the condition
before executing the loop body.
2 for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop
variable.
3 do...while loop
It is more like a while statement, except that it tests the condition at the end of the loop body.
4 nested loops
You can use one or more loops inside any other while, for, or do.while loop.
1. Counter controlled loops: The type of loops, where the number of the execution is
known in advance are termed by the counter controlled loop. That means, in this case,
the value of the variable which controls the execution of the loop is previously
known. The control variable is known as counter. A counter controlled loop is also
called definite repetition loop.
Example: A while loop is an example of counter controlled loop.
========
sum = 0;
n = 1;
while (n <= 10)
{
sum = sum + n*n;
n = n+ 1;
}
========
This is a typical example of counter controlled loop. Here, the loop will be executed exactly
10 times for n = 1,2,3, ......,10.
Sentinel controlled loop: The type of loop where the number of execution of the loop
is unknown, is termed by sentinel controlled loop. In this case, the value of the control
variable differs within a limitation and the execution can be terminated at any moment
as the value of the variable is not controlled by the loop. The control variable in this
case is termed by sentinel variable.
Example: The following do.... while loop is an example of sentinel controlled loop.
do
{
printf(“Input a number.\n”);
scanf("%d", &num);
}
while(num>0);
======
In the above example, the loop will be executed till the entered value of the variable num is
not 0 or less than 0. This is a sentinel controlled loop and here the variable num is a sentinel
variable.
while loop first check the condition do-while first enter the body and
then enter the body. then check the condition.
while is an entry -controlled loop. do-while is an exit-controlled
loop.
In a while, condition comes before In do-while, condition comes
the body. after the body.
While loop:
A while loop statement repeatedly executes a target statement as long as a given condition is
true.
Syntax:
while(condition)
statement(s);
Here, statement(s) may be a single statement or a block of statements. The condition may be
any expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following
the loop.
Fig 2
Key point of the while loop is that the loop might not ever run. When the condition is tested
and the result is false, the loop body will be skipped and the first statement after the while
loop will be executed.
Lab Task
#include <iostream>
int main ()
int n;
n=1;
while(n<=5)
cout<<”pakistan”;
n++;
getch();
#include <iostream>
int main ()
int n;
n=1;
while(n<=10)
{
cout<<n<<endl;
n++;
getch();
#include <iostream>
int main ()
int n, sum ;
sum = 0;
n = 1;
sum = sum + n;
n++;
cout<<”sum is “<<sum<<endl;
getch();
4. Write Program to Find Table Of Inputted Number Using While Loop.
#include<iostream>
#include<conio.h>
int main()
int n,c,tab;
cin>>n;
c=1;
cout<<"Table of "<<num<<endl;
while(c<=10)
tab=num*c;
c++;
getch();
5. Write Program to Find Table of Inputted Number Using While Loop till specific
limit.
#include<iostream>
#include<conio.h>
int main()
{
int n,c,tab,limit;
cin>>n;
cin>>limit;
c=1;
cout<<"Table of "<<num<<endl;
while(c<=limit)
tab=num*c;
c++;
getch();
#include<iostream.h>
#include<conio.h>
int main()
clrscr();
int count;
while(count>=1)
cout<<" "<<count;
count--;
getch(); }
Do-while loop
The do...while loop checks its condition at the bottom of the loop.
Syntax:
do
statement(s);
}
while(condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in
the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the
loop execute again. This process repeats until the given condition becomes false.
LAB SESSION