Vous êtes sur la page 1sur 25

CONTROL

STRUCTURES
while
do while
for statement

DECISION MAKING AND LOOPING


CONTROL STRUCTURES
ITERATIVE OR REPETITIVE
while statement and example programs
do while statement and example programs
for statement and example statements
break and continue statements

7/9/2015

Department of CSE

Iterative (loop) control structures


Iterative (repetitive) control structures are used to repeat
certain statements for a specified number of times.
The statements are executed as long as the condition is true
These kind of control structures are also called as loop control
structures
Three kinds of loop control structures:
while
do while
for

7/9/2015

Department of CSE

Iterative (loop) control structures


Each loop control structure will have
Program loop: body of loop.
control statement tests certain conditions & then
directs repeated execution of statements within the body
of loop.
Two types: Based on position of control statement.
1) Entry controlled loop: control is tested before the start of
the loop. If false, body will not be executed.
2) Exit controlled loop: test is performed at the end of the
body. i.e. body of loop executed at least once.

7/9/2015

Department of CSE

Entry Controlled & Exit controlled loops


Entry

Test
Condition

Entry

Body of
The loop

False

True

Body of
The loop

Test
Condition

True

False

7/9/2015

Department of CSE

While statement
Basic format:
while (test condition)
{
body of the loop
}
Entry controlled loop statement
Test condition is evaluated & if it is true, then body of the
loop is executed.
After execution, the test condition is again evaluated & if it is
true, the body is executed again.
This is repeated until the test condition becomes false, &
control transferred out of the loop.
Body of loop is not executed if the condition is false
at the very first attempt.
7/9/2015

Department of CSE

Example
#include <iostream.h>

void main( )
{
int counter;
counter =1; // initialization of count variable

while (counter <= 5)


{
}

cout << "I love computers << endl;


counter = counter + 1;

7/9/2015

Department of CSE

Program to find the sum of natural


numbers up to N
#include <iostream.h>
void main( )
{
int n;
int sum = 0; //initialize sum
int i=1;
cout<<Enter a value for n : ;
cin>>n;

while (i <= n)
while (n<=100)
{{
sum=
sum=sum+n
sum + i ;
in= i +1;
= n +1;
}}
}

cout<<sum;

7/9/2015

Department of CSE

Program to find the average marks scored for


unknown number of students in 3 subjects
and display whether passed or failed ( average
passing marks is 65).
char cChoice = 'y';
float fMark1,fMark2,fMark3;

while( (cChoice == 'Y') || (cChoice == 'y') ) {

cout<<"Enter the marks scored by the student in 3 subjects\n";


cin>>fMark1>>fMark2>>fMark3;
fSum = fMark1 + fMark2 + fMark3;
fAvg = fSum / 3;
if ( fAvg >= 65.0)
cout<<"Student PASSED\n";
else
cout<<"Student FAILED\n";
cout<<"Do you wish to continue? Enter Y/N or y/n :";
cin>>cChoice; //cChoice = getch();

7/9/2015

Department of CSE

What is the output of the following


code snippet?
unsigned int iCount = 1;
while (iCount<10);
{
iCount = iCount + 1;
cout<<iCount;
}

7/9/2015

Because of the ;

Department of CSE

10

To understand while loop

7/9/2015

Department of CSE

11

The do statement
General form:
do
{

body of the loop


}
while (test condition);

7/9/2015

Department of CSE

12

The do statement
General form:
do
{

body of the loop


}
while (test condition);

Exit controlled loop.


After do statement, program executes the body of the Loop.
At the end of the loop, the test condition is evaluated.
If it is true, body of the loop is executed once again & this
process continues as long as the condition is true.
When condition becomes false, the loop will be terminated.
Body of the loop is executed at least once.
do while loop can be nested.
7/9/2015

Department of CSE

13

Example: Finding sum of natural


numbers up to 100
#include <iostream.h>
void main()
{
int n;
int sum;
sum=0; //initialize sum
n=1;

do
do
{{
sum
sum==sum
sum++n;counter;
ncounter
= n +1; = counter +1;}}
} } while (n
< 100);< 100);
(counter

#include <iostream.h>
void main( )
{
int n;
int sum;
sum=0; //initialize sum
n=1;

while (n<100)
{
sum=sum+n;
n = n +1;
}

cout<<sum;

cout<<sum;

}
7/9/2015

}
Department of CSE

14

Example
int iNumber, iSum = 0;
do {
cout<<Enter a number. Type 0(zero) to EXIT ;
cin>>iNumber;
iSum = iSum + iNumber;
} while (iNumber != 0);

7/9/2015

Department of CSE

15

The for statement


The general form:

for (initialization; test condition; increment)


{
Body of the loop
}
Next statement;
Entry controlled loop.

7/9/2015

Department of CSE

16

Example: Finding sum of natural


numbers up to 100
#include <iostream.h>
void main()
{
int n;
int sum;
sum=0; //initialize sum

for(n = 1; n < 100; n=n + 1)


{
sum=sum + n;
}

#include <iostream.h>
void main( )
{
int n;
int sum;
sum=0; //initialize sum
n=1;

while (n < 100)


{
sum= sum + n;
n = n + 1;
}

cout<<sum;
}

cout<<sum;
}

7/9/2015

Department of CSE

18

Find largest of 3 numbers


Algorithm: Largest of 3 numbers
Step 1:
Step 2:

Input A,B,C
LA

Step 3:

If B>L then
L B
if C>L Then
LC

Step 4:
Step 5:
Step 6:

7/9/2015

Print L
[End of Algorithm]
STOP
Department of CSE

19

Compute factorial of a number


Algorithm: Factorial of a Number
Step1: Input Num
Step 2: fact 1
Step 3: For count=1 to Num in step of 1 do
begin
fact fact * count
count count + 1
end
Step 4: Print fact of Num=, fact
Step 5: [end of algorithm]
Stop

7/9/2015

Department of CSE

20

Program to find the factorial of a num


#include <iostream.h>
#include <conio.h>
void main( )
{
int num;
int count, fact;
fact =1;
clrscr();
cout<<Enter a value for num : ;
cin>>num;

for(count = 1; count<=num; count ++)


{
fact = fact * count;
}
cout<<Factorial of <<num<< = <<fact;
getch();

7/9/2015

Department of CSE

21

Find the sum and mean of first N


natural numbers
Algorithm:
Step1:

sum and mean of natural numbers.


Input N

Step 2:

Sum 0

Step 3:

for Num=1 to N in step of 1 do


begin
Sum Sum + Num
end
Mean Sum / N
Print Sum of N natural numbers=,Sum
Print Mean of N natural numbers =,Mean
[End of algorithm]
Stop

Step 4:
Step 5:

Step6:
7/9/2015

Department of CSE

22

Algorithm to count the no of digits of


a number
Algorithm:
Step 1:
Step 2:
Step 3:

Step 4 :
Step 5: :

7/9/2015

count the no of digits.


input N
count 0
while N > 0
begin
NN/10 (integer)
count count + 1
end
Print count=, count
[End of Algorithm]
STOP
Department of CSE

23

Algorithm to calculate sum of digits


of a number
Algorithm: find sum of digits of a number
Step 1: Input N
Step 2: sum0
Step 3: while N > 0
begin
rem N mod 10
sum sum + rem
NN/10 (integer)
end
Step 4: print sum
Step 5 :[End of Algorithm]
Stop
7/9/2015

Department of CSE

24

Algorithm for Fibonacci series


Algorithm : Fibonocci Series
Step 1 : Input Limit
Step 2: First0,Second1
Step 3: print First
Step 4: while Second < Limit
begin
Print Second
Next First + Second
FirstSecond
Second Next
end
Step 5:[End of Algorithm]
Stop
7/9/2015

Department of CSE

25

Summary
while
do while
for statement

7/9/2015

Department of CSE

26

Vous aimerez peut-être aussi