Introduction to Programming
INTRODUCTION TO PROGRAMMING
Chapter 7
Loops
1
Introduction to Programming
Loops
Topics
• Loops
• Condition Tested
Loops
• Counted Loops
• Endless Loops
• FOR Loop
• WHILE Loop
• DO-WHILE Loop
2
Introduction to Programming
Loops
Loops
• A loop is a sequence of instructions that is
continually
repeated until a certain condition is reached.
• Loops allow for the same statement to be
executed a number of times in succession.
3
Introduction to Programming
Loops
Loops
Pseudocod Flowchar
e: t:
Fals
Condition e
Loop
Do Something Tru
Until Condition e
Do something
4
Introduction to Programming
Loops
Loops
• There are three types which are commonto
most
programming languages:
– Condition Tested Loops
– Counted Loops
– Endless Loops
5
Introduction to Programming
Loops
Condition Tested Loops
• A condition tested loop is one which repeats a
set of instructions until a certain condition is
reached.
• The test can be performed at the start of the
loop (before any of the instructions are
executed), during the loop, or at the end of
the loop.
• Usually, the condition will be testing the result
of executing the statements that are inside
the loop. 6
Introduction to Programming
Loops
Counted Loops
• A counted loop is one which allows the
programmer to instruct the computer to
perform a set of instructions x times, where x
is usually an integer value, but some
programming languages offer other data
types.
• One could argue that the counted loop is just a
condition tested loop which updates a counter
and exits once a given value is reached.
7
Introduction to Programming
Loops
Counted Loops
• The only time to use a count loop is when
the program can determine ahead of time
how many times the loop will repeat.
• There are generally two ways that the
number of
repetitions of a loop will be know ahead of
time:
– The loop always repeats the same
number of times.
– The program calculates the number of
repetitions based upon user input. 8
Introduction to Programming
Loops
Endless Loops
• An endlessloop goes roundand rounduntil
one of
three things happens:
– The computer is turned off (or the
application stopped, forcefully)
– The computer encounters an EXIT
(or similar) statement
– An error forces the application to 'crash'
• Some endless loops serve a purpose, in
message loops, for example, where it is
necessary to continually monitor for incoming 9
Introduction to Programming
Loops
Example of Loop Statement
• These are examples loop statement in
programming
language
– FOR Loop
– WHILE Loop
– DO … WHILE Loop
10
Introduction to Programming
Loops
FOR Loop
• A FOR loop is a loop that repeats a specified
number
of times.
• The loop uses a counter to tell it how many
times to run the same sequence of activities.
11
Introduction to Programming
Loops
FOR Loop
• The counter has the following three numeric
values:
– Initial counter value
– Increment (the amount to add to the
counter each time the loop runs)
– Final counter value
• The loop ends when the counter reaches the
final counter value, or, if there is an
associated test condition, when the test
condition is true.
12
Introduction to Programming
Loops
FOR Loop
Pseudocod Flowchar
e: t:
FOR x times Initial value
Do Something
Increment Conditio
n
(Final
counte
r)
Do something
Increment
13
Introduction to Programming
Loops
FOR Loop
FOR loop syntax:
FOR (initial counter value, final counter,
increment) Statement (Do Something)
14
Introduction to Programming
Loops
FOR Loop
Example 1:
Output:
FOR (x=1, x<5, x++)
PRINT “Hello Hello
World” World
Hello
World
Hello
World
Hello
World 15
Introduction to Programming
Loops
FOR Loop
Example 2:
FOR (x=1, x<=4, Output:
x++) PRINT x
1
2
3
4
16
Introduction to Programming
Loops
FOR Loop
Example 3:
FOR (x=5, x>0, Output:
x--) PRINT x
5
4
3
2
1
17
Introduction to Programming
Loops
WHILE Loop
• A WHILE loop is a loop that repeats while
some
condition is satisfied.
• The WHILE loop tests its condition at the
beginning of every loop.
• If the condition is false from the start, the
sequence of activities contained in the loop
never runs at all.
18
Introduction to Programming
Loops
WHILE Loop
Pseudocod Flowchar
e: t:
WHILE condition
Fals
Do Conditio e
Something n
Tru
e
Do something
19
Introduction to Programming
Loops
WHILE Loop
WHILE loop syntax:
WHILE (Condition)
Statement (Do
Something)
20
Introduction to Programming
Loops
WHILE Loop
Example 1:
WHILE (x < 5) Output:
PRINT “Hello
Hello
World” x++
World
Hello
World
Hello
World
Hello
World
21
Introduction to Programming
Loops
WHILE Loop
Example 2:
Output:
WHILE (key != Esc)
Hello
PRINT “Hello
world
World” Hello
world
Hello
world
Hello
world
… 22
Introduction to Programming
Loops
DO-WHILE Loop
• Like a while loop, a do-while loop is a loop
that
repeats while some condition is satisfied.
• Unlike a while loop, a do-while loop tests its
condition at the end of the loop.
• This means that its sequence of activities
always runs at least once.
23
Introduction to Programming
Loops
DO-WHILE Loop
Pseudocod Flowchar
e: t:
D
o something Do something
WHILE condition
Fals
e
Condition
True
24
Introduction to Programming
Loops
DO-WHILE Loop
DO-WHILE Loop
Syntax
DO
Statement
WHILE
(Condition)
25
Introduction to Programming
Loops
DO-WHILE Loop
Example 1:
x=
1
Output:
DO
PRINT “Hello Hello
World” x++ World
WHILE (x<5) Hello
World
Hello
World
Hello
26
World
Introduction to Programming
Loops
DO-WHILE Loop
Example 2:
x=
Output:
1
DO
PRINT “Hello Hello World
World” x++
WHILE (x>5)
27
Introduction to Programming
Loops
DO-WHILE Loop
Example 3:
DO Output:
PRINT “Hello
World” WHILE (Key != Hello
Esc) World
Hello
World
Hello
World
… 28
Introduction to Programming
Loops
Exercise
Draw flowchart diagram for the following
programs using
loop:
1. A program that display number 1 to 20
2. A program that display a person name x
times.
29