PLF Lesson 3 - Loop
PLF Lesson 3 - Loop
Looping
Subject: Program Logic Formulation
Instructor: Kim Shaira Casimiro
The advantages Using a loop
of looping control variable
Avoiding
Nested loops common loop
mistakes
OBJECTIVES
Using a for loop
Presentation Title
Looping 2
Understanding the Advantages of Looping
Although making decisions is what
makes computers seem intelligent, looping
makes computer programming both efficient and
worthwhile. When you use a loop, one set of
instructions can operate on multiple, separate
sets of data. Using fewer instructions results in
less time required for design and coding, fewer
errors, and shorter compile time. Recall the loop
structure that you learned about in Chapter 3; it
looks like Figure 5-1. As long as a Boolean
expression remains true, the body of a while
loop executes. Quick Reference 5-1 shows the
pseudocode standards this book uses for the
while statement
3
Advantages of Looping
5
TWO TRUTHS & A LIE
1. When you use a loop, you can write one set of instructions that
operates on multiple, separate sets of data.
2. A major advantage of having a computer perform complicated
tasks is the ability to repeat them.
3. A loop is a structure that branches in two logical paths before
continuing
6
Using a Loop Control Variable
7
Using a Loop Control Variable
Three separate actions should occur using a loop control variable:
9
10
• Within a loop’s body, you can change the value of the loop control variable in a
number of ways. For example: You might simply assign a new value to the loop
control variable. You might retrieve a new value from an input device.
Looping
• You might increment, or increase, the loop control variable, as in the logic in
Figure 5-3. You might reduce, or decrement, the loop control variable. For
example, the loop in Figure 5-3 could be rewritten so that count is initialized to
4, and reduced by 1 on each pass through the loop. The loop would then
continue while count remains greater than 0.
• The terms increment and decrement usually refer to small changes; often the
value used to increase or decrease the loop control variable is 1. However, loops
are also controlled by adding or subtracting values other than 1. For example, to
display company profits at five-year intervals for the next 50 years, you would
want to add 5 to a loop control variable during each iteration. Because you
frequently need to increment a variable, many programming languages contain a
shortcut operator for incrementing. For example, in C++, C#, and Java, the
expression ++value is a shortcut for the expression value = value + 1. You will
learn about these shortcut operators when you study a programming language
that uses them.
11
A counter is any numeric variable that counts the number of times an
event has occurred. In everyday life, people usually count things starting
with 1. Many programmers prefer starting their counted loops with a
variable containing 0 for two reasons: In many computer applications,
numbering starts with 0 because of the 0-and-1 nature of computer
circuitry. When you learn about arrays in Chapter 6, you will discover
that array manipulation naturally lends itself to 0-based loops.
12
Using an Indefinite Loop with a Sentinel Value
13
Using a Loop Control
Variable
14
Figure 5-5 shows how the program might look when it is executed at the
command line and in a GUI environment. The screens in Figure 5-5 show
programs that perform exactly the same tasks using different environments. In
each environment, the user can continue choosing to see Hello messages, or
can choose to quit the program and display Goodbye
15
Nested Loops
Program logic gets more complicated when you must use loops within loops, or
nested loops. When one loop appears inside another, the loop that contains the
other loop is called the outer loop, and the loop that is contained is called the inner
loop. You need to create nested loops when the values of two or more variables
repeat to produce every combination of values. Usually, when you create nested
loops, each loop has its own loop control variable
16
17
Figure 5-8 shows the logic for the program that produces answer sheets. Three loop control variables are
declared for the program:
18
19
20
TWO TRUTHS & A LIE
Nested Loops
1. When one loop is nested inside another, the loop that contains
the other loop is called the outer loop.
2. You need to create nested loops when the values of two or more
variables repeat to produce every combination of values.
3. The number of times a loop executes always depends on a
constant.
Presentation title 21
Avoiding Common Loop Mistakes
Programmers make the following common mistakes with loops:
1. Failing to initialize the loop control variable
2. Neglecting to alter the loop control variable
3. Using the wrong type of comparison when testing the loop control
variable
4. Including statements inside the loop body that belong outside the
loop
22
Thank you for
listening!