Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

LAB No.

10
IMPLEMENTATION OF ITERATIVE STATEMENT IN C++

PRE LAB TASK

Objectives:

 To implement for loop


 To implement while and do while loop.

Introduction:

A loop statement allows us to execute a statement or group of statements multiple


times.

Loop: (Fig 1)
Theory:

Loop Type & Description:

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.

Difference between the while and do while loop:

while loop do-while

 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:

The syntax of a while loop in C++ is:

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.

Flow Diagram: Fig 2

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

1. Write a program using while loop to print Pakistan.

#include <iostream>

#include <conio.h >

int main ()

int n;

n=1;

while(n<=5)

cout<<”pakistan”;

n++;

getch();

2. Write a program using while loop to print counting till ten.

#include <iostream>

#include <conio.h >

int main ()

int n;

n=1;

while(n<=10)

 
 
{

cout<<n<<endl;

n++;

getch();

3. Write a program using while loop for sum / forward counting.

#include <iostream>

#include <conio.h >

int main ()

int n, sum ;

sum = 0;

n = 1;

while(n <= 10)

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;

cout<<"Enter the number To Find Table"<<endl;

cin>>n;

c=1;

cout<<"Table of "<<num<<endl;

while(c<=10)

tab=num*c;

cout<<num<<" * "<<c<<" = "<<tab<<endl;

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;

cout<<"Enter the number to Find Table"<<endl;

cin>>n;

cout<<"Enter the limit"<<endl;

cin>>limit;

c=1;

cout<<"Table of "<<num<<endl;

while(c<=limit)

tab=num*c;

cout<<num<<" * "<<c<<" = "<<tab<<endl;

c++;

getch();

6. Write a program using while loop for backward counting.

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int count;

cout<<"The first twenty integers in reverse order are as follows:"<<endl;


count=20

while(count>=1)

cout<<" "<<count;

count--;

getch(); }

Do-while loop

The do...while loop checks its condition at the bottom of the loop.

Syntax:

The syntax of a do...while loop in C++ is:

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.

Flow Diagram: Fig 3


Fig 3

LAB SESSION

1. Write a program to reverse any given integer number.


For Example: Number 13520: 02531
2. Write a program to calculate HCF of Two given number.
3. Write a program to calculate the sum of following series where n is input
by user.
 1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

You might also like