Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Algorithms

…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

IF score >= 75 THEN


INSTRUCTION(S) TO FOLLOW IF THE
CONDITION IS MET. PRINT “You passed!”

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.

VAR variable_name:data type ; If (condition) then


BEGIN begin
<instructions> <instructions>;

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

Write code to accept the Write code to read the name


number of hours worked and score received by a
each week by an student. If the score is
employee. If the number between 0 and 50 (inclusive),
of hours worked per print “Academic probation”. If
week exceeds 40 hours the score is between 51 and 79
print a message “You (inclusive) print ”Needs
earned overtime pay” Improvement”, otherwise
otherwise print “You print “Excellent work!” along
earned regular pay”. with the student's name.
Provide solutions for the following using Code

Write code to accept the Write code to read the miles


travelled by a traveling
number of boys and the officer. If the number of miles
number of girls in a class. exceeds 12,000 then the
travelling claim will be
If the number of boys
5000+(number of miles -
exceeds the number of 12,000) multiplied by 1.5,
girls it should display otherwise the travelling
claim will be $5000.00.
”The boys win!” Display the travel claim
amount for the traveling
officer.
Loops

🞺 FOR
🞺 WHILE
🞺 REPEAT….UNTIL
What is ?
• A loop allows us to repeat a
block of code.

• As long as the condition


associated with the loop is true,
the block of code will continue to
be repeated.
Main Types of Loops
• INDEFINITE LOOP
– A “while” loop
– A “repeat-until” loop

• 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

FOR var ← start TO end DO For var← 1 TO 3

Instructions
Print ”Hello!”

Calculation to Increase or decrease counter variable


var← var + 1
ENDFOR

STOP
The anatomy (parts) of a for loop
Counter variable
START

DECLARE count AS INTEGER


Starting and ending values
FOR count ← 1 TO 10 DO

Counter variable update to PRINT “Hello”


increases or decrease counter count ← count + 1
variable value

ENDFOR
Instructions to be repeated
STOP
Put the blocks in the correct order to produce a pseudocode to count
from 1 to 10.
START

DECLARE counter AS INTEGER

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

PRINT “The total price for the items is”,total

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

STOP total← total + 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.

• As long as the condition


associated with the “while” loop
is true, the block of code will be
executed and the loop will
“WHILE” Loop Layout
Variable with Start_Value
declared and assigned before
while loop
WHILE ( condition goes here ) DO
do something
do something else
ENDWHILE
“WHILE” Loop Example
count 🡨 0
WHILE ( count < 5 ) DO
count 🡨 count + 1
PRINT count
ENDWHILE
“WHILE” Loop Example
An integer variable
named count is
count 🡨 0 declared and set
equal to zero.
WHILE ( count < 5 ) DO
count 🡨 count + 1
PRINT count
ENDWHILE
“WHILE” Loop Example
count 🡨 0 A while loop is
created with the
WHILE ( count < 5 ) DO condition, count < 5,
and the condition is
count 🡨 count + 1 checked.
PRINT count
ENDWHILE
“WHILE” Loop Example
count 🡨 0 As long as count
remains less than 5,
WHILE ( count < 5 ) DO the loop will run.
count 🡨 count + 1
PRINT count
ENDWHILE
“WHILE” Loop Example
count 🡨 0
WHILE ( count < 5 ) DO
Inside the loop, the
count = count + 1value of count is
increased by one,
PRINT count (from 0 to 1).

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.

You might also like