PRAYER
Most Sacred Heart of Jesus,
Have mercy on us.
Most Sacred Heart of Jesus, We trust in
you. Most Sacred
Heart of Jesus, Make our
heart’s light unto yours.
Amen.
Let’s have a
recap!
Computer 9
LESSON 4
USING WHILE
LOOP STATEMENTS
Prepared by: Mr. Dennis S. Santos, LPT
Learning Objectives:
At the end of the lesson, we should be able to:
1. Identify the while loop statement and its syntax.
2. Use the while loop statement in creating
programs.
3. Demonstrate accuracy when constructing test
conditions.
While Loop Statement
The while loop is used to repeat a statement
or group of statements while a given condition
is True. It tests the condition before executing
the body of the loop.
If we do not know beforehand the number of
times to iterate, we generally use the while
loop.
While Loop Statement
The while loop is used to repeat a statement
or group of statements while a given condition
is True. It tests the condition before executing
the body of the loop.
If we do not know beforehand the number of
times to iterate, we generally use the while
loop.
• The key point of the while
loop is that the loop
might not ever run. When
the condition is tested
and the result is False,
the body of the loop will
be skipped. The first
statement after the while
loop will be executed.
• The syntax of while loop is:
while condition:
statement(s)
• The body of the while loop must be indented. The
statements may be a single statement or a block of
statements. The condition may be any expression.
The loop iterates while the condition is True.
• When the condition becomes False, the program
control passes to the statement immediately after
the loop.
Example 1: Numbers from 1 to 5
Let us create a program that displays the numbers
from 1 to 5.
Pseudocode:
1. Initialize number=0
2. If number is less than 5
Increment the value of number by 1.
Display number
Repeat step 2
Else
Exit loop
Program ends
Flowchart
:
Program:
Output:
• The initial value of the variable n is set
to 0.
• The condition while n < 5 checks to
see if n < 5 The first time the program
evaluates this test expression, n is 0,
and O is less than 5.
• In other words, while n is less than 5,
the program will execute the
statements in the while loop.
Example 2: Sum of Natural Numbers
Let us create a program that computes the sum of the
natural numbers from 1 to n, where n' is the number
entered by the user.
Program:
Output:
This is the output if n = 5.
1 sum= 0 +
1 n= 5
=1
2 sum= 1 +
1
1 + 2 + 3 + 4 + 5 =
3 sum=
=2
2 + 15
1
= 3
sum= 3 +
4
1
=4
5 sum= 4 +
1
=5
Output:
• In the program above, the condition while
counter <= n: will be True as long as the variable
counter is less than or equal to n, where n is the
number entered by the user.
• It is very important to increase the value of the
counter variable in the body of the loop. Otherwise,
it will result in an infinite loop (never-ending
loop), which we will learn in the succeeding
lessons.
Did you know?
NATURAL NUMBERS are used to count
objects. The set of natural numbers, denoted by
N, can be defined in either of two ways:
N= {0, 1, 2, 3, ...} nonnegative integers or
whole numbers
N = {1, 2, 3, 4, ...} positive integers
Example 3: Message
Let us create a program that keeps asking the user to
input a message, displays back the message, and lets
the user choose when to end the program.
Program:
Output:
Output:
• The variable message is set to an empty string, so the
program enters the loop.
• The program then asks the user to input a message.
Whatever is entered will be stored in the variable and
displayed.
• The program evaluates the condition in the while
statement again. As long as the user does not enter the
word 'exit', the program will keep on asking the user to
input a message.
• When the user finally enters 'exit' the program stops
executing the while loop and the program ends.
While Loop with Else Statement
Just like the for loop, we can have an optional else
block with while loop as well. If the condition in the
while loop is evaluated as False, the else part is
executed.
The while loop can also be terminated with a break
statement. In such case, the else part is ignored.
Therefore, the else part runs if no break occurs and if
the condition is False.
While Loop with Else Statement
The syntax of while loop with else statement is
while condition:
statement(s)
else:
statement(s)
When the condition becomes False, the statement in
the else block s executed.
Example 4: Program:
Numbers from 1 to 5
Let us create a program
that displays the
numbers from 1 to 5.
Output:
Output:
• The while loop displays the numbers from 1 to 5
while the condition is True
• When the condition becomes False, it executes the
block of code in the else statement and displays
the message 5 is not less than 5.
Computer 9
Thank You,
Class!