session3 - iteration
TRANSCRIPT
-
8/14/2019 Session3 - Iteration
1/14
Session 3
Iteration
-
8/14/2019 Session3 - Iteration
2/14
Iteration / 2 of 14
Objectives Understand Loops
Discuss WHILE loop
Discuss DO. WHILE loop
Discuss the REPEAT . UNTIL loop
Work with the FOR loop
-
8/14/2019 Session3 - Iteration
3/14
Iteration / 3 of 14
Loops A loop is a set of statements that are repeated a
certain number of times.
The block of statements written in between theloop gets executed as many number of times as theuser wants
The four types of loops are:
The WHILE loop The DO..WHILE loop The REPEATUNTIL loop The FOR loop
-
8/14/2019 Session3 - Iteration
4/14
Iteration / 4 of 14
The While Loop The WHILE loop repeats statements while a
certain specified condition is True
The syntax:
WHILE condition
DO
statement set
END DO
-
8/14/2019 Session3 - Iteration
5/14
Iteration / 5 of 14
An ExampleBEGIN
cnt = 0
WHILE (cnt< 1000)DO
DISPLAY Scooby
cnt = cnt+1
END DO
END
-
8/14/2019 Session3 - Iteration
6/14
Iteration / 6 of 14
The DOWHILE Loop In the DO WHILE loop the body of the code is executed
once before the test is performed
The general form of the DO WHILE loop isDO
Statements
WHILE conditions
When the condition becomes False in a DO WHILE theloop will be terminated, and the control goes to the
statement that appears immediately after the WHILE
statement.
-
8/14/2019 Session3 - Iteration
7/14Iteration / 7 of 14
An Example
BEGIN
DO
DISPLAY Enter a number
[enter 5 to exit]:
INPUT num
WHILE (num 5)
END
START
DISPLAY "Enter a number [enter 5 to exit]:"
INPUT nu m
num 5
STO P
-
8/14/2019 Session3 - Iteration
8/14Iteration / 8 of 14
The REPEATUNTIL Loop The statements within the loop body get executed
before the condition is evaluated
The loop in the REPEAT.UNTIL loop executesuntil the condition evaluates to true
The general syntax is
REPEAT
Statement-set
UNTIL condition
-
8/14/2019 Session3 - Iteration
9/14Iteration / 9 of 14
An Example
BEGIN
REPEAT
DISPLAY Enter a num [enter 5 to exit]:
INPUT num
UNTIL (num=5)
END
START
DIS PLAY "Enter a num ber [enter 5 to exit]:"
INPUT num
num = 5
S T OP
-
8/14/2019 Session3 - Iteration
10/14Iteration / 10 of 14
The FOR Loop The FOR loop consists of a single line which will include three parts:
Initialization
Condition
Increment / Decrement
The general syntax for a FOR loop is
FORcounterVariable IN RANGEstartvalue to endValue [STEP value]
DO
statement setEND DO
ThestartValue and endValue denote the initial and final values of the counter
-
8/14/2019 Session3 - Iteration
11/14Iteration / 11 of 14
An Example
BEGIN
FOR cnt IN RANGE 1 to 1000
DO
DISPLAY Scooby
END DO
END
-
8/14/2019 Session3 - Iteration
12/14Iteration / 12 of 14
Nested Loops A Nested loop is one loop within another
BEGIN
cnt = 1
cnt2 = 1WHILE cnt < 6
INPUT num
cnt2 = 1
WHILE cnt2 < 3
DO
PRINT numcnt2 = cnt2 + 1
END DO
cnt = cnt + 1
END DO
END
-
8/14/2019 Session3 - Iteration
13/14Iteration / 13 of 14
Breaking out of a Loop To exit from loop, the keyword breakis used
The need to break the loop arises when a search is conducted through a big list. In this case if the
required information is found the first time, then the search need not be continued
BEGIN
DISPLAY To exit type in 999
FOR a IN RANGE 1 to 5
DO
INPUT num
IF num = 999
EXIT LOOP
END IF
DISPLAY num
END DO
END
-
8/14/2019 Session3 - Iteration
14/14Iteration / 14 of 14
Skipping a part of a Loop Skipping certain parts of the loop is done when certain conditions have to be excluded
from the program
BEGINFOR a IN RANGE 1 to 20
DO
IF a= 13
CONTINUE
END IFDISPLAY a
END DO
END