Вы находитесь на странице: 1из 12

Objective: Students will be able

to demonstrate While - loops

WHILE LOOPS using Pseudocode and


Flowcharts
OBJECTIVES
State the purpose of looping constructs
Name the two categories of loops(Definite and indefinite)
IdentifyWHILE Loops as an example of indefinite loop
Write the syntax for a WHILE loop
Explain the operation of a WHILE loop construct
Deduce questions for the application of WHILE
Use a WHILE loop construct to solve a simple problem
CONDITION-CONTROLLED LOOP
A while loop is controlled by a condition
We use this type of loop when we want a condition to be
repeated (iterated) until a particular goal is achieved or until
the process has to stop.
In a while loop, the action will repeat WHILE a condition is true
If the test is false, then there will be no more iterations
P.S If the test is false at the start of the process, there will be
no iterations at all. Thus, in certain conditions, the actions inside
the loop will not be repeated, even once.
STRUCTURE OF WHILE-LOOP
Initial value for condition (this can be stored in a variable that is read or it can be
assigned)

WHILE (Logical test/Condition) DO


Actions if true
ENDWHILE
QUESTION 1
Create algorithm (pseudocode and flowchart)
to ask a student to input a password
If the password is not equal to ‘Sesame’ output
‘ Wrong password, try again’ else output
‘correct password’
PSEUDOCODE
Start
Declare password as string
Read password
While password <> ‘Sesame’ Do
Print “wrong password, try again”
Read password
Endwhile
Print “correct password”
Stop
PASCAL
Program Pass_word;
VAR
password : string
BEGIN
Writeln(‘To have the user enter the correct password’);
Writeln(‘ Please enter your password’);
Readln( password);
While password <> ‘Sesame’ Do
BEGIN
Writeln(‘wrong password, try again’)
Writeln(‘Enter your password);
Readln(password);
End;
Writeln(‘correct password’);
END.
FLOW CHART

<>
QUESTION 2
A teacher wants to write a computer program to test
knowledge of Mathematics. Create algorithm (pseudocode and
flowchart) that sets out the following sequence of instructions.
Output what is 56+98?
Input an answer
If the correct answer is inputted, say “Yes, well done and stop”.
Otherwise,’ “No, try again” and repeat the action.
PSEUDOCODE
Start
Declare answer, ans_wer as integer
Print “Please enter your answer”
read answer
Ans_wer= 98 + 56
While ans_wer <> answer
Print “ No, try again”
Read answer
Endwhile
Output ‘Yes, Well done’
Stop
QUESTION 3 - HOMEWORK
Write algorithm (pseudocode and flowchart) to
calculate and print the average of 3 scores for a
number of students terminated by 9.
PLEASE NOTE
Example a bank will pay out money to a customer until his account is empty. We do
not know how many times he can withdraw money but we know when it will stop.
This means that when you are using a WHILE loop
If the test is true, the loop will continue

Вам также может понравиться