Professional Documents
Culture Documents
If, If Then, Nested If Selection Statements & Loops
If, If Then, Nested If Selection Statements & Loops
…continued
What is a
Selection
A selection statement allows you
Statement?
to choose which action to perform
based on a condition.
•IF…THEN
–Performs an action if a condition is true
•IF…THEN…ELSE
–Different actions are done. One action is
done if the condition is true and a different
action is done if the condition is false.
What is a
Selection
•Nested IF
Statement?
statements
IF statements may be
combined or NESTED in
other if statements. In other
words, you can have an IF
statement in another IF
construct.
FORMAT OF SELECTION STATEMENT - IF THEN
START
DECLARE score AS INTEGER
CONDITION
PRINT “Please enter score”
READ score
ENDIF
STOP
START
DECLARE score AS INTEGER
PRINT “Please enter score”
READ score
IF score >= 75 THEN
PRINT “You passed!”
ENDIF
STOP
Program select; START
var score: INTEGER; DECLARE score AS
BEGIN INTEGER
WRITELN(‘Please enter score’); PRINT “Please enter score”
READLN (score); READ score
IF (score >= 75) THEN IF score >= 75 THEN
BEGIN PRINT “You passed!”
WRITELN (‘You passed!’); ENDIF
END;
STOP
END.
If (condition) then
Pascal Format
<instruction>;
PROGRAM <title> ; Format for if statement with just one
instruction.
END . <instructions>;
end; Format for if statement with multiple
instructions.
Pascal Format - if…then...else
If (condition) then If (condition) then
begin
<instruction>
<instructions>;
Else End
<instruction>; Else
Begin
<instructions>;
end;
Provide solutions for the following using Code
🞺 FOR
🞺 WHILE
🞺 REPEAT….UNTIL
What is ?
• A loop allows us to repeat a
block of code.
• DEFINITE LOOP
– A “for” loop
PATTERN OF A LOOP
• Input starting value using a
variable, this value determines if a
loop should start/continue.
• Test the variable against some
condition
• Carry out the instructions in the
loop
• Update the loop variable’s value.
How about here? What is being repeated here?
For Loop
The for loop allows the repeat of a
set of instructions a specific
number of times.
The for loop has a special variable
known as the counter variable
which keeps a check on the number
of times the set of actions inside
the loop is carried out or executed.
Format of the for loop
Instructions
Print ”Hello!”
STOP
The anatomy (parts) of a for loop
Counter variable
START
ENDFOR
Instructions to be repeated
STOP
Put the blocks in the correct order to produce a pseudocode to count
from 1 to 10.
START
FOR counter← 1 TO 10 DO
PRINT counter
counter← counter+1
ENDFOR
STOP
WRITE START
PSEUDOCODE TO
PRINT THE SUM DECLARE num, sum AS
OF THE NUMBERS
1 TO 5.
Complete by sum← 0
filling in the num ← TO DO
blanks.
sum ← sum +
num ← num +
ENDFOR
PRINT
STOP
WRITE PSEUDOCODE
TO PRINT YOUR
FAVOURITE MEAL
20 times.
WRITE START
PSEUDOCODE TO
READ 20 ITEM DECLARE price, total AS REAL
PRICES AND i AS INTEGER
PRINT THE TOTAL
PRICE FOR ALL total ← 0
20 ITEMS. FOR i ← 1 TO 20 DO
PRINT “Please enter a price”
READ price
total← total + price
i← i + 1
ENDFOR
STOP
NOW YOU WRITE A
PSEUDOCODE
QUESTION WHICH
REQUIRES A FOR
LOOP. PROVIDE
YOUR SOLUTION IN
THE BOX
PROVIDED.
NOW DRAW
FLOWCHARTS FOR
THE QUESTIONS
ON SLIDES 10
and 11.
START
START
DECLARE PRICE, TOTAL AS REAL
DECLARE price, total AS REAL I AS INTEGER
i AS INTEGER
total ← 0 total← 0
FOR i ← 1 TO 20 DO
PRINT “Please enter a price” For i← 1 TO 20
READ price
total← total + price
i← i + 1 PRINT”Enter price”
ENDFOR
PRINT “The total price for the items is”,total READ price
i← i + 1
PRINT total
In your own
words what is
the
difference(s)
between the
WHILE Loop and
the FOR Loop.
The “WHILE” Loop
• The “while” loop is a very simple
loop.
ENDWHILE
“WHILE” Loop Example
count 🡨 0
WHILE( count < 5 ) DO
count 🡨 count + 1
PRINT count The current value of
Output: 1 count is printed to
the screen..
“WHILE” Loop Example
count 🡨 0
WHILE ( count < 5 ) DO
count 🡨 count + 1
PRINT count
ENDWHILE The ENDWHILE indicates we have
Output: 1 reached the end of the block
and the end of the 1st run or
“iteration” through the loop.
“WHILE” Loop Example
count 🡨 0 The condition is
checked again with
WHILE ( count < 5 ) DO the current value of
count (which is 1).
count 🡨 count + 1
PRINT count
ENDWHILE
Output: 1
“WHILE” Loop Example
count 🡨 0
Since count < 5 (1 <
WHILE ( count < 5 ) 5), the loop will run
again.
count 🡨 count + 1
PRINT count
ENDWHILE
Output: 1
“WHILE” Loop Example
count 🡨 0
WHILE( count < 5 ) DO Inside the loop, the
value of count is
count 🡨 count + increased
1 by one,
(from 1 to 2).
PRINT count
ENDWHILE
Output: 1
“WHILE” Loop Example
count 🡨 0
WHILE ( count < 5 ) DO
{
count 🡨count + 1
PRINT count The current value of
Output: 1 count is printed to
2
} the console.
“WHILE” Loop Example
count 🡨 0
WHILE ( count < 5 ) DO
count 🡨 count + 1
PRINT count
ENDWHILE The ENDWHILE indicates we have
Output: 1 reached the end of the block
2
and the end of the 2nd
iteration of the loop.
“WHILE” Loop Example
count 🡨 0 The condition
is checked
WHILE ( count < 5 ) DO again with
the current
count 🡨 count + 1 value of
count (which
PRINT count is 2).
ENDWHILE
Output: 1
2
“WHILE” Loop Example
count 🡨 0
WHILE ( count < 5 ) DO
count = count + 1
PRINT count
ENDWHILE
Output: 1 The process continues and our
2 output continues to update each
3
time the loop is run (until the
4
5 value of count is 5).
“WHILE” Loop Example
count 🡨 0 After the 5th
iteration, count
WHILE ( count < 5 ) DO = 5, which
means the
count = count + 1 condition is not
true (5 < 5 is
PRINT count false).
ENDWHILE
Output: 1
2
3
4
5
“WHILE” Loop Example
The loop is
count 🡨 0 complete and
the program
WHILE ( count < 5 )DO will continue
to any code
count 🡨 count + 1 given after
the block
PRINT count statement
(after
ENDWHILE
Output: 1 ENDWHILE).
2
3
4
5
Problem 1
• What is the output of the loop? (Pay
attention to the small change.)
count 🡨 0
WHILE( count < 5 )DO
PRINT count
count = count + 1
ENDWHILE
Problem 2
• What is the OUTPUT of the loop
and the FINAL VALUE of run?
run 🡨 7
WHILE ( run < 10 ) DO
PRINT run
run 🡨 run+1
Repeat….Until
Repeat
<instructions>
<update statement>
until(condition)
WHILE vs REPEAT….UNTIL
WHILE vs REPEAT….UNTIL
WHILE vs REPEAT….UNTIL
Your turn!
Write an algorithm to accept an
unspecified amount of integers.Calculate
and print the sum of the numbers
entered. The program should be
terminated when the number 999 is
entered.