0% found this document useful (0 votes)
79 views23 pages

PLF Lesson 3 - Loop

The document discusses different types of loops in programming. It covers the advantages of using loops, which include writing fewer instructions to operate on multiple data sets and reducing errors and compile time. It describes using a loop control variable to manage the number of repetitions in a loop. Specific types of loops covered include definite loops using counters, and indefinite loops using sentinel values. Nested loops are discussed for when two or more variables need to repeat combinations of values. Common mistakes with loops like failing to initialize or alter the control variable are also outlined.

Uploaded by

Vert Valencia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views23 pages

PLF Lesson 3 - Loop

The document discusses different types of loops in programming. It covers the advantages of using loops, which include writing fewer instructions to operate on multiple data sets and reducing errors and compile time. It describes using a loop control variable to manage the number of repetitions in a loop. Specific types of loops covered include definite loops using counters, and indefinite loops using sentinel values. Nested loops are discussed for when two or more variables need to repeat combinations of values. Common mistakes with loops like failing to initialize or alter the control variable are also outlined.

Uploaded by

Vert Valencia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lesson 3:

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

You can use a while loop to execute a body of


statements continuously as long as some condition
continues to be true. The body of a loop might contain
any number of statements, including module calls,
selection structures, and other loops. To make a while
loop end correctly, you can declare a loop control
variable to manage the number of repetitions a loop
performs.

7
Using a Loop Control Variable
Three separate actions should occur using a loop control variable:

1. The loop control variable is initialized before entering the loop.


2. The loop control variable’s value is tested, and if the result is true, the loop body is
entered.
3. The loop control variable is altered within the body of the loop so that the tested
condition that follows while eventually is false.

Commonly, you can control a loop’s repetitions in one of two ways:


1. Use a counter to create a definite, counter-controlled loop.
2. Use a sentinel value to create an indefinite loop
Using a Definite Loop with a
Counter
Figure 5-3 shows a loop that displays Hello four
times. The variable count is the loop control
variable. This loop is a definite loop because it
executes a definite, predetermined number of
times—in this case, four. The loop is a counted
loop, or counter-controlled loop, because the
program keeps track of the number of loop
repetitions by counting them.

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

Often, the value of a loop control variable is not altered by arithmetic,


but instead is altered by user input. For example, perhaps you want to keep
performing some task while the user indicates a desire to continue. In that
case, you do not know when you write the program whether the loop will be
executed two times, 200 times, or at all. This type of loop is an indefinite loop.
Consider an interactive program that displays Hello repeatedly as long as the
user wants to continue. The loop is indefinite because each time the program
executes, the loop might be performed a different number of times. The
program appears in Figure 5-4

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!

You might also like