Pseudo Code

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 100

INTRODUCTION

TO
PSEUDOCODE
VARIABLES & CONSTANTS
These occupy a space in
memory that holds data
that a program might use
or manipulate.
VARIABLES
These values can change
throughout the algorithm as data
is processed.

Each variable is given a name so


values can be assigned to them.
VARIABLES
To assign a value
to a variable:
Total = number1 +
number 2
OR
Total → number1 +
number 2
CONSTANT
Type of variable where the
value does not change for
that algorithm.
EXAMPLES:
VAT 20% or PI 3.14
IPO CHARTS
TO
PSEUDOCODE
*Input required for processing

*Processing

*Output

*Always end your statement


*Input required for processing

*Processing

*Output

*Always end your statement


INPUT PROCESSING OUTPUT
EXAM CHECK IF IF TRUE
MARK EXAM_MARK >= 90 OUTPUT
"GRADE A"

INPUT exam_mark *Input required for processing

IF exam_mark >= 90 *logical test done using input

THEN OUTPUT “Grade A” *action if true

ENDIF *always end your statement


INPUT PROCESSING OUTPUT
number1 Add the three If true then
number2 numbers together output the total
number3 Check if the
total > 50

READ number1, number2, number3


Total = number1 + number2 + number3
IF Total > 50
THEN PRINT Total
ENDIF
IF Logical Test
THEN Actions if True
ENDIF
LOGICAL OPERATORS
• Arithmetic
Operators

• Relational
Operators
CONDITIONAL STATEMENTS
• Every conditional statement starts
with a logical test.
• Yes/No OR True/False
IF…THEN
Actions to be carried out if TRUE.

IF Logical Test

THEN Actions if True

ENDIF
IF Logical Test
THEN Actions if True
ENDIF

← CONSTANT
← VARIABLE
IF…THEN…ELSE
Actions to be carried out if TRUE
AND
Actions to be carried out if FALSE

IF Logical Test

THEN Actions if True

ELSE Actions if False

ENDIF
IF Logical Test
THEN Actions if True
ELSE Actions if False
ENDIF
INPUT age
IF age > 12
THEN OUTPUT “You may buy War Machine”
ELSE OUTPUT “ You may not buy War Machine”
ENDIF
Rakesh wants to buy chocolate. He
has decided that if his allowance is
$10 or more, he will buy chocolate.
If his allowance is less than $10, he
will not buy chocolate. Write this in
pseudocode.
INPUT allowance
IF allowance >= 10
THEN OUTPUT “Buy chocolate”
ELSE OUTPUT “ Do not buy chocolate”
ENDIF
Darryl wants to cool down because
it’s a really hot day. If the
temperature is less than 35
degrees, he will go to the beach,
otherwise he will go to the pool.
Write this in pseudocode.
INPUT temperature
IF temperature < 35
THEN OUTPUT “Go to the beach”
ELSE OUTPUT “Go to the pool”
ENDIF
The year is 2022. Input the year Anita’s
dog Spike was born. Calculate the dog’s
age. If the dog is 8 years or older, he
needs to take special supplements. If he
is younger than 8 years, he needs to get
a booster shot. Write this in pseudocode.
year = 2022
INPUT birth_year

*KEYWORDS are written in CAPITAL


letters*

*variables are written in common


letters*
year = 2022
INPUT birth_year
age = year – birth_year

*the variable age is a necessary


input so it must be calaulated*
year = 2022
INPUT birth_year
age = year – birth_year
IF age >= 8

*Perform logical test*


year = 2022
INPUT birth_year
age = year – birth_year
IF age >= 8
THEN OUTPUT “Needs supplements”

*Output actions if the logical


test is TRUE*
year = 2022
INPUT birth_year
age = year – birth_year
IF age >= 8
THEN OUTPUT “Needs supplements”
ELSE OUTPUT “Needs booster shot”

*Output actions if the logical


test is FALSE*
year = 2022
INPUT birth_year
age = year – birth_year
IF age >= 8
THEN OUTPUT “Needs supplements”
ELSE OUTPUT “Needs booster shot”
ENDIF

*Never forget to end your


statement!*
If a student scores 50 or more in an
exam then output the student score
and that they passed. Otherwise,
output the score and that they
failed. Write this in pseudocode.
INPUT exam_score

*KEYWORDS are written in CAPITAL


letters*

*variables are written in common


letters*
INPUT exam_score
IF exam_score > 50

*Perform logical test*


INPUT exam_score
IF exam_score > 50
THEN OUTPUT exam_score, “Passed”

*Output actions if the logical


test is TRUE*
INPUT exam_score
IF exam_score > 50
THEN OUTPUT exam_score, “Passed”
ELSE OUTPUT exam_score, “Failed”

*Output actions if the logical test


is FALSE*
INPUT exam_score
IF exam_score > 50
THEN OUTPUT exam_score, “Passed”
ELSE OUTPUT exam_score, “Failed”
ENDIF

*Never forget to end your


statement!*
A worker is paid a rate of $90 per
day and works 6 days per week.
Input the rate of pay and number of
days worked. Calculate and print
the weekly salary paid to a worker.
INPUT rate
INPUT days_worked

*KEYWORDS are written in CAPITAL


letters*

*variables are written in common letters*


INPUT rate
INPUT days_worked
weekly_salary = rate * days_worked

*the variable weekly_salary is a


necessary input so it must be
calaulated*
INPUT rate
INPUT days_worked
weekly_salary = rate * days_worked
PRINT weekly_salary

*PRINT was specified as the


output method*
Include a bonus of $150
only if the weekly salary is
more than $450. Print the
final salary. Otherwise print
that the worker does not
qualify for a bonus.
INPUT rate
INPUT days_worked
weekly_salary = rate * days_worked
bonus = $150
PRINT weekly_salary

*bonus is initialized as a constant

weekly_salary needs to be calculated before


bonus since it will be used first*
INPUT rate
INPUT days_worked
weekly_salary = rate * days_worked
bonus = $150
PRINT weekly_salary
IF weekly_salary > $450

*Perform logical test*


INPUT rate
INPUT days_worked
weekly_salary = rate * days_worked
bonus = $150
PRINT weekly_salary
IF weekly_salary > $450
THEN final_salary = weekly_salary + bonus
PRINT final_salary

*Output actions if the logical test is TRUE*


INPUT rate
INPUT days_worked
weekly_salary = rate * days_worked
bonus = $150
PRINT weekly_salary
IF weekly_salary > 540
THEN final_salary = weekly_salary + 150
PRINT final_salary
ELSE PRINT “Worker does not qualify for a bonus”

*Output actions if the logical test is


FALSE*
INPUT rate
INPUT days_worked
INPUT bonus = 150
weekly_salary = rate * days_worked
PRINT weekly_salary
IF weekly_salary > 450
THEN final_salary = weekly_salary + bonus
ELSE OUTPUT “does not qualify for a bonus”
PRINT final_salary
ELSE PRINT “Worker does not qualify for a bonus”
ENDIF

*Never forget to end your statement!*


IF Logical Test
THEN Actions if True
NESTED IF
ELSE IF Actions if FALSE (Next Logical Test)
THEN Actions if True
ENDIF
For a score greater than 60,

student has gotten a credit.

For a score greater than 50,

student has gotten a pass.


INPUT score

*KEYWORDS are written in CAPITAL


letters*

*variables are written in common


letters*
INPUT score

IF Score > 60

*Perform logical test*


INPUT score

IF Score > 60

THEN OUTPUT “ Credit”

*Output actions if the logical test is


TRUE*
INPUT score
IF Score > 60
THEN OUTPUT “ Credit”
ELSE IF Score > 50

*Actions if first logical test is


FALSE : Perform second
logical test*
Answer:
IF Score > 60
THEN OUTPUT “ Credit”
ELSE IF Score > 50
THEN OUTPUT “Pass”

*Output actions if second logical test is


TRUE
INDENT! Since this is a nested statement*
IF Score > 60
THEN OUTPUT “ Credit”
ELSE IF Score > 50
THEN OUTPUT “Pass”
ENDIF
*Never forget to end your statement!*
Accept any two
numbers as input.
Divide the larger by
the smaller then
print the answer.
INPUT number1, number2

*KEYWORDS are written in CAPITAL


letters*

*variables are written in common letters*


INPUT number1, number2
IF number1 > number2

*Perform logical test*


INPUT number1, number2
IF number1 > number2
THEN answer = number1 / number2

*Output actions if the logical test is


TRUE*
INPUT number1, number2
IF number1 > number2
THEN answer = number1 / number2
ELSE IF number2 > number1

*Actions if first logical test is


FALSE: Perform second logical
test*
INPUT number1, number2
IF number1 > number2
THEN answer = number1 / number2
ELSE IF number2 > number1
THEN answer = number2 / number1

*Output actions if second logical test is


TRUE
INDENT! Since this is a nested statement*
INPUT number1, number2
IF number1 > number2
THEN answer = number1 / number2
PRINT answer
ELSE IF number2 > number1
THEN answer = number2 / number1
PRINT answer
ENDIF

*Never forget to end your statement!*


If a car can hold 5
or more people,
then output that
the customer will
rent the car.
INPUT car_capacity
IF car capacity >= 5
THEN OUTPUT “Customer will rent the car”
ENDIF
If a basket costs $25
or less then print buy
the basket, if it costs
more then print do
not buy basket.
INPUT basket_cost
IF basket_cost <= 25
THEN PRINT “Buy the basket”
ELSE PRINT “Do not buy the basket”
ENDIF
If a basket costs $25
dollars or less then
print buy now. If the
basket costs $30 or
less then print maybe
later.
INPUT basket_cost
IF basket_cost <= $25
THEN PRINT “Buy the basket”
ELSE IF basket_cost <= $30
THEN PRINT “Maybe later”
ENDIF
Accept the names and
scores of 11 batsmen on a
cricket team. Determine
and print the name of the
batsman who scored the
most runs as well as the
one who scored the least.
low_score = 10 000
high_score = 0
high_score_batsman = null
low_score_batsman = null
FOR batsman 1 TO 10 DO
INPUT score
INPUT batsman_name
IF score > high_score THEN
high_score = score
high_score_batsman = batsman_name
ENDIF
IF score < low_score THEN
low_score = score
low_score_batsman = batsman_name
ENDFOR
PRINT “High score of” high_score “made by” high_score_batsman
PRINT “Low score of” low_score “made by” low_score_batsman
LOOPS

• Allows one or more actions to be repeated.

• Each repetition is called an ITERATION.

• There are two types of loops, the difference


between them is how the loop stops.

• All loops must include a way of stopping.


1) COUNTER-CONTROLLED LOOPS
• The loop stops after a fixed number
of times repeated.
• Must know HOW MANY TIMES the
loop needs to be repeated.
• FOR Statements
THE COUNTER

• A variable is used as the ‘counter’.

• Stores a value that increases with


each iteration.
FOR Number of Counts DO *Loop repeats a
Actions
fixed number of
ENDFOR
times*
FOR employee 1 to 5 DO Activity:
INPUT hours • What variable is
INPUT rate the counter?
pay = hours * rate
• How many
OUTPUT pay
iterations?
ENDFOR
Put these actions within a counter-
controlled loop which will iterate for
100 employees. Choose a suitable
name for the counter variable.
INPUT pay
tax = pay * 0.2
OUTPUT tax
FOR employees 1 TO 100 DO

INPUT pay

tax = pay * 0.2

OUTPUT tax

ENDFOR
There are 10 students
in a class. Each student
gets marks on an essay
and marks on a test.
Output the total mark
for each student.
FOR students 1 TO 10 DO

INPUT student_name, essay_mark,

test_mark

total_mark = essay_mark + test_mark

OUTPUT student_name, total_mark

ENDFOR
There are 15 students
in a class. Input the
score for each
student. Determine
and print the highest
score.
high_score = 0
high_score_student = null
FOR students 1 TO 15 DO
INPUT student_name, score
IF score > high_score
THEN high_score = score
high_score_student = student_name
ENDIF
ENDFOR
PRINT high_score_student, high_score
Enter 10 numbers and
calculate the total.
total = 0
FOR count 1 TO 10 DO
INPUT number
total = total + number
ENDFOR
OUTPUT total
Enter 10 numbers and
find the largest
number.
largest_number = 0
FOR count 1 TO 10 DO
INPUT number
IF number > largest_number
THEN largest_number = number
ENDIF
ENDFOR
OUTPUT largest_number
Find the average
exam mark out of 5
subjects. Print the
answer.
total = 0
count = 0
FOR subjects 1 to 10 DO
INPUT exam_mark
sum = sum + exam_mark
count = count + 1
ENDFOR
average = sum/count
PRINT average
2) CONDITION-CONTROLLED LOOPS
• A condition needs to be met to stop the loop

• This type of loop is stopped by a Yes/No or


True/False test.
• The number of repeats is unknown.

• Two types of condition-controlled loops:


WHILE – condition tested at beginning
REPEAT – condition tested at end
WHILE Condition is True
Actions if True
ENDWHILE
• Condition is tested at the beginning of the
statement
• If the test is true, the loop will run.
• If it is not true, the loop will not run.
• If the test becomes false, there will be no
more iterations.
WHILE Conditional Test DO
Actions if True
ENDWHILE

INPUT password
WHILE password <> ‘Sesame’ DO
OUTPUT ‘Wrong password, try again’
INPUT Password
ENDWHILE
OUTPUT ‘Correct Password’
• Output “What is 40 + 60”
• Input an answer
• If the correct answer is input
say
“Yes, well done and stop
• Otherwise output “No, try again”
and repeat the actions
OUTPUT “What is 40 + 60?”
INPUT Answer
WHILE Answer <> 100 DO
OUTPUT “No, try again”
INPUT Answer
ENDWHILE
OUTPUT “Yes, well done”
Print: Enter a number greater
than 10. Accept an answer. If
the answer is less than 10,
print: enter a number greater
then 10. If the answer is greater
than 10, print: “Well done!”
PRINT “Enter a number greater than 10”
INPUT answer
WHILE answer < 10 DO
PRINT “Enter a number greater then 10”
INPUT answer
ENDWHILE
PRINT “Well done”
REPEAT
Statement
UNTIL Condition is True

• Condition is tested at the end of the


statement
• Will always be executed at least once.
• Executes 1 or more statements as long
as the condition is false.
REPEAT
Statement
UNTIL Condition is True
• Add all the even numbers
between 1 and 20
• Display the sum
Sum = 0
Count = 1
REPEAT
IF Count + 1 is even
THEN Sum = Sum + Count
Count = Count + 1
UNTIL Count > 20
DISPLAY Sum
Sum = 0
Count = 1
REPEAT
IF Count + 1 is even
THEN Sum = Sum + Count
Count = Count + 1
UNTIL Count > 20
DISPLAY Sum
Set the temperature
increase to 20 degrees.
Increase the temperature
by the same amount until
the temperature is 100
degrees.
THE END

You might also like