0% found this document useful (0 votes)
11 views

Quarter-4-Lesson-1-Looping-with-while-statements-student-copy

This lesson covers the concept of looping in programming, focusing on while statements, their advantages, and the use of loop control variables. It distinguishes between definite and indefinite loops, explains nested loops, and highlights common mistakes to avoid when implementing loops. Key examples illustrate how to manage loop control variables and utilize sentinel values effectively.

Uploaded by

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

Quarter-4-Lesson-1-Looping-with-while-statements-student-copy

This lesson covers the concept of looping in programming, focusing on while statements, their advantages, and the use of loop control variables. It distinguishes between definite and indefinite loops, explains nested loops, and highlights common mistakes to avoid when implementing loops. Key examples illustrate how to manage loop control variables and utilize sentinel values effectively.

Uploaded by

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

Lesson 1 – Quarter 4

z
LOOPING WITH
while
STATEMENTS
Jean T. Tumaneng
In this lesson, you will learn about:
z

 The advantages of looping

 Using a loop control variable

 Difference between definite and


indefinite loops
 Nested loops

 Avoiding common loop mistakes


z

while - do - endwhile
LOOP
or
structure
while - endwhile
z
z
Understanding
the Advantages
of Looping
Loop
z

 one set of instructions can operate on


multiple, separate sets of data
 fewer instructions used results in less
time required for design and coding,
fewer errors, and shorter compile time
 looping makes computer programming
both efficient and worthwhile
z
Using a Loop
Control Variable
z

Loop control variable

 Manages the number of


repetitions a loop performs
z
Two (2) types of LOOP:

1. Definite Loop - executes a definite,


predetermined number of times /
repetitions (counter loop, counter-
controlled loop)
2. Indefinite Loop – performs the task while
the user indicates a desire to continue
z

Using a Definite Loop with a


Counter

Counter is any numeric variable


that counts the number of
times an event has
occurred.
Three (3) separate actions should occur
z
1. The loop control
using a loop control variable:
start
variable is Loop control
initialized before variable
Declarations Loop control variable
entering the loop. num count
is initialized.
=0
2. The loop control
variable’s value is
YES
tested, and if the count < 4?

result is true, the NO output “Hello”


loop body is
entered. Loop control variable
is tested. count = count + 1
3. The loop control
variable is altered output
“Goodbye”
within the body of
the loop so that the Loop control variable
stop
tested condition that is altered.

follows while
eventually is false.
start
Declarations
num count = 0
while count < 4
output
“Hello”
count =
count + 1
endwhile
output
“Goodbye”
Ways to change the value of the Loop
z Control Variable
start

 Simply assign Declarations


num count
a new value to =0

the loop
YES
control count < 4?

variable NO output “Hello”

count = 4

output
“Goodbye”

stop
Ways to change the value of the Loop
z Control Variable
start

 Retrieve a Declarations
num count
=0
new value
from an count < 4?
YES

input device NO output “Hello”

input count

output
“Goodbye”

stop
Ways to change the value of the Loop
z Control Variable
start

 Increment (+
Declarations
num count
+), or =0

increase, the
YES
loop control count < 4?

variable NO output “Hello”

count = count + 1

output
“Goodbye”

stop
Ways to change the value of the Loop
z Control Variable
start

Declarations
 Reduce, or num count
=8
decrement(--),
the loop control YES
count > 4?
variable
NO output “Hello”

count = count - 1

output
“Goodbye”

stop
z

Using an Indefinite Loop


with a Sentinel Value

Sentinel  A preselected value that stops


Value
the execution of a program
 it represents an entry or exit
point
start
z
Declarations
string RESPONSE
Loop control variable
is initialized.

output “Do you want to


continue? Y or N >>”

input RESPONSE

RESPONSE YES
= “Y”?

NO output “Hello”

Loop control variable output “Do you want to


output
is tested. continue? Y or N >>”
“Goodbye”

Loop control variable


input RESPONSE is altered.
stop
start
Declarations
string RESPONSE
output “Do you want to continue? Y or N >>”
input RESPONSE
while RESPONSE = “Y”
output “Hello”
output “Do you want to continue? Y or
N >>”
input RESPONSE
endwhile
output “Goodbye”
stop
z
Nested LOOP
Outer Loop -
Nested Loop
z
the loop that
contains the
 a loop inside another loop
other loop

YES

NO

YES

NO
Inner Loop -
the loop that
is contained
a=2
a=2
z
b=4
b=4 while a < 5
output “Thank you!”
NO
a < 5?
YES c=1
while c < b
output
output
“Goodbye
“Thank you” output “Come
!”
Again!”
c=1
c=c+1
endwhile
NO
c < b?
YES a=a+1
a=a+1 output
endwhile
“Come
Again”
c=c+1
output “Goodbye”
a=2
z b=4
while a < 5
output “Thank you!”
c=1
while c < b
output “Come
Again!”
c=c+1
endwhile
a=a+1
endwhile
Avoiding
z
Common Loop Mistakes

Programmers make the following common mistakes


with loops:
 Failing to initialize the loop control variable

 Neglecting to alter the loop control variable

 Using the wrong type of comparison when testing


the loop control variable
 Including statements inside the loop body that
belong outside the loop

You might also like