Professional Documents
Culture Documents
PPT-Session 6
PPT-Session 6
nl
O
se
U
tre
en
C
h
ec
pt
Session 6 – Iteration
rA
Constructs
Fo
y
nl
O
Explain looping constructs
se
Describe the different types of loops
U
Explain nested loops
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
Sometimes, a computer
se
program needs to perform
U
some tasks repetitively
tre
en
To perform these tasks, the
C
h
iterative or looping construct
ec
is used
pt
rA
Fo
y
nl
O
Consider that a programmer has to write a
se
program that displays a name 1000 times.
U
tre
The programmer cannot type the DISPLAY
en
statement 1000 times.
C
h
ec
can be used.
Fo
y
nl
O
The example shows the pseudocode to add an
se
iteration construct to the DISPLAY
U
statement.
tre
en
Do loop 1000 times
C
DISPLAY “Mike”
End loop
h
ec
pt
rA
Fo
y
nl
O
Different types of loops supported by most
se
programming languages are as follows:
U
The WHILE Loop
tre
The DO…WHILE Loop
en
The REPEAT…UNTIL Loop
The FOR Loop C
h
ec
pt
rA
Fo
y
nl
O
The WHILE loop repeats a statement or a set of
se
statements until a certain specified condition is
U
met or is true.
tre
en
The general form of the WHILE loop is as follows:
C
h
WHILE condition
ec
DO
pt
statement set
rA
END DO
Fo
y
nl
O
The modified pseudocode for the earlier example
using the WHILE loop is as follows:
se
U
BEGIN
tre
cnt=0
en
WHILE (cnt < 1000)
DO C
h
DISPLAY “Mike”
ec
cnt=cnt+1
pt
END DO
rA
END
Fo
y
nl
O
The flowchart for the
se
pseudocode is shown in
U
the figure.
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
In C, the syntax for the WHILE loop is as
se
follows:
U
tre
while(condition)
en
{
C
h
statement set
ec
}
pt
rA
Fo
y
nl
O
In C, the example would be written as follows:
se
#include <stdio.h>
U
void main()
tre
{
int cnt;
en
cnt=0;
while(cnt<1000) C
h
{
ec
printf(“Mike”);
pt
cnt++;
rA
}
}
Fo
y
nl
O
The WHILE loop tests the condition before the body of
se
the loop is executed.
U
Therefore, the body of the loop may not be executed at
tre
all if the condition is not satisfied in the first attempt.
en
C
On some occasions, it might be necessary to execute
h
ec
se
follows:
U
tre
DO
en
Statements
C
h
WHILE condition
ec
pt
rA
Fo
y
nl
O
Consider an example where the loop displays a
se
message and accepts a number from the user.
U
The loop continues doing this until the user enters the
tre
number 5 and then, exits the program.
en
The pseudocode for this is as follows:
BEGIN C
h
DO
ec
INPUT num
rA
se
figure.
U
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
The FOR loop provides a more concise loop
se
control structure.
U
tre
The general form for a FOR loop is as follows:
en
C
FOR counterVariable IN RANGE startValue
h
to endValue [STEP value]
ec
DO
pt
statement set
rA
END DO
Fo
y
nl
O
Consider the earlier example to display a
se
name 1000 times.
U
tre
BEGIN
en
FOR cnt IN RANGE 1 to 1000
DO C
h
ec
DISPLAY “Mike”
pt
END DO
rA
END
Fo
y
nl
O
The C syntax for the FOR loop along with its
se
execution path is shown in the figure.
U
tre
en
C
h
ec
pt
rA
Fo
y
nl
O
The FOR loop construct for displaying the name
se
Mike 1000 times in C language is shown in the
U
example.
tre
en
…
C
for(cnt=0;cnt<1000;cnt++)
h
{
ec
printf(“Mike”);
pt
}
rA
…
Fo
y
nl
O
Nested loop:
se
Is a loop within another loop.
U
tre
Is used in almost all programming languages.
en
C
Can have upto 15 levels in C language to be nested
h
within one another.
ec
pt
programming languages.
Fo
y
nl
An example of a nested WHILE loop where the program accepts five
O
numbers from the user and displays them twice on the screen is
se
shown.
U
BEGIN
cnt = 1
tre
cnt2 = 1
en
WHILE cnt < 6
INPUT num
cnt2 = 1
WHILE cnt2 < 3 C
h
DO
ec
PRINT num
pt
cnt2 = cnt2 + 1
END DO
rA
cnt = cnt + 1
END DO
Fo
END
y
nl
O
The iterative or looping construct is used to repeat certain steps a
se
specific number of times or till some specified condition is met.
The different types of loops are the WHILE loop, the DO…WHILE
U
loop, the REPEAT…UNTIL loop, and the FOR loop.
tre
The WHILE loop repeats a statement or a set of statements while a
en
certain specified condition is True.
The DO…WHILE loop executes the body of the loop before the
C
condition test is performed.
h
ec
The FOR loop provides a more concise loop control structure, which
rA