Académique Documents
Professionnel Documents
Culture Documents
STRUCTURES
while
do while
for statement
7/9/2015
Department of CSE
7/9/2015
Department of CSE
7/9/2015
Department of CSE
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
7/9/2015
Department of CSE
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
7/9/2015
Department of CSE
7/9/2015
Because of the ;
Department of CSE
10
7/9/2015
Department of CSE
11
The do statement
General form:
do
{
7/9/2015
Department of CSE
12
The do statement
General form:
do
{
Department of CSE
13
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
7/9/2015
Department of CSE
16
#include <iostream.h>
void main( )
{
int n;
int sum;
sum=0; //initialize sum
n=1;
cout<<sum;
}
cout<<sum;
}
7/9/2015
Department of CSE
18
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
7/9/2015
Department of CSE
20
7/9/2015
Department of CSE
21
Step 2:
Sum 0
Step 3:
Step 4:
Step 5:
Step6:
7/9/2015
Department of CSE
22
Step 4 :
Step 5: :
7/9/2015
23
Department of CSE
24
Department of CSE
25
Summary
while
do while
for statement
7/9/2015
Department of CSE
26