Goal: Understand that a name holds a specific value.
Look at the following piece of Python code:
score = 10
name = "Alex"
What value is stored in the variable score ?
What value is stored in the variable name ?
Goal: Understand that variables can only hold one value at a time.
Look at the following piece of Python code:
score = 10
score = 20
score = 5
print (score)
Which value will be displayed on the screen after the last line of code is executed?
Goal: Understand looking up a value.
Look at the following piece of Python code:
x = 5
y = x
What is the value assigned to the variable x ?
What value is stored in the variable y ?
The final code looks like this:
x = 5
y = x
x = 12
print (y)
What value will be displayed when the last line of code is executed?
Goal: Master the Right-to-Left logic.
Look at the following piece of Python code:
points = 10
points = points + 1
Explain what happens in the second line.
If print points were added to the code on the left, what would be displayed on the screen?
Goal: Track changes over time.
Look at the following piece of Python code:
a = 3
b = 4
c = a + b
a = c
b = a - 2
Complete the tracetable for the code given in the left. Step 1 row is done for you as an example.
Step | a | b | c |
|---|---|---|---|
1 | 3 | ||
2 | |||
3 | |||
4 | |||
5 |
Address these explicitly before they become habits.
Trap: 10 = score
Fix: The container (variable) must always be on the left. You cannot put a variable inside a number. However, you can place (store) a value into a variable (the containter.
Trap: Thinking y = x links them forever.
Fix: Assignment is a snapshot in time. It copies the value right now, not the relationship.
In the previous page, you saw one of the most famous and practical algorithms called a 'COUNTING' algorithm.
But 'COUNTING' algorithms are not the only ones in which variables update themselves.
Look at this one:
01 DECLARE no_items : INTEGER
02 DECLARE item_price , total_price : REAL
03 DECLARE loop_counter : INTEGER
04 item_price, total_price <- 0.0
05 OUTPUT "Enter the number of items to check out: "
06 INPUT no_items
07 FOR loop_counter <- 1 TO no_items
08 OUTPUT "Enter the price of the item: "
09 INPUT item_price
10 total_price <-- total_price + item_price
11 NEXT loop_counter
12 OUTPUT "Total price for the items is: ", total_price
area = width * height
width = 5
height = 10
print(area)
Why will this code likely crash or print "0" (or null)?
a and b so a becomes 3 and b becomes 9.The Broken Code:
a = 9
b = 3
a = b
b = a
print(a, b)
If you run this, both a and b end up as 3. Why didn't they swap?
How do you fix it?
This line ( x = 12) is added to the piece of code on the left. Will it change the value stored in variable y ?
When designing algorithms and delivering them in pseudocode, variables must be defined and declared at the top.
Variables local to procedures and functions must be declared inside the definition body of the procedure/ function.
Depending of what we are going to store in the variable, we must declare its data type as well.
Here are data types and where they are used:
INTEGER : e.g. 25 , -123
REAL : e.g. 12.45, -0.14
CHAR : e.g. 'A' , 'g' , '#'
STRING : e.g. 'Alex' , 'address'
BOOLEAN : e.g. TRUE , FALSE
DATE : e.g. 12/09/2022
TIME : e.g. 12:34
CURRENCY : $21.95
Here is how variables are defined and declared: (Pseudocode as per instructions by Cambridge)
Example 1:
DECLARE Age : INTEGER
Age <- 17
Example 2:
DECLARE Name : STRING
OUTPUT "What is your name?"
INPUT Name
Pay attention to how the keyword DECLARE and the data type STRING are written in capital letters. Keywords in pseudocode are always written in capital. Look at INPUT and OUTPUT. They follow the same rule.
In example 1, a memory location in the memory is labelled Age, and in the next line the integer value 17 is stored in that location.
In example 2, a memory location in the memory is labelled Name, and two lines further, that location is used to collect an input data from the keyboard and store it.
Define a variable called DoB and define its data type. This variable is going to be used to store data of birth of the employee.
Send a message to the user and ask them to enter their date of birth in DD/MM/YY format.
Use the DoB variable to collect the user data from the keyboard.
Display the following message on the screen:
"You entered <display their date of birth here> as you date of birth. Recorded."
The following algorithm is designed to calcuate the area of a circle. Put the psedocode lines in the corrent order.
area <- r * r * 3.14
INPUT r
OUTPUT "Enter the radius of the circle: "
DECLARE area , r : REAL
OUTPUT "The area of the circle is: ", area
The following algorithm is designed to calcuate a final price. Put the psedocode lines in the corrent order.
DECLARE units : INTEGER
INPUT units
OUTPUT "Enter the number of of notebooks you would like to purchase: "
unit_price <- 2.55
discount <- 0.8
DECLARE discount : REAL
total_price <- unit_price * units
DECLARE unit_price , total_price : REAL
IF units > = 20 THEN total_price <- total_price * discount ENDIF
OUTPUT "Total to be paid: ", total_price
OUTPUT
'N'
basket
INPUT
DECLARE
continue
item
CHAR
The famous SWAP algorithm.
There are two variables available for Player1 and Player2.
Write pseudocode that swaps the values stored in the variables without losing any of the values.
For example, if Player1 holds the value of "Mike" and Player2 holds the value of "Jared", write pseudocode that places "Jared" in Player1 and "Mike" in Player2.
Look at the following pseudocode:
01 DECLARE pos_count : INTEGER
02 DECLARE user_number : INTEGER
03 DECLARE loop_counter : INTEGER
04 pos_count <- 0
05 FOR loop_counter <- 1 TO 20
06 OUTPUT "Enter an integer number either negative or positive: "
07 INPUT user_number
08 IF user_number > 0 THEN
09 pos_count <- pos_count + 1
10 ENDIF
11 NEXT loop_counter
12 OUTPUT "You entered ", pos_count , "positive integers!"
What name would you choose for the algorithm given?
Which line controls the number of iterations of the loop?
Which line is an example of a variable updating itself?
Which code line is responsible for incrementing the variable loop_counter?
How many numbers will the user have the chance to enter as input?
What purpose do lines 08-10 serve in this algorithm?
Which single line is responsible for keeping a record of positive numbers?
If you wanted to count the negative numbers as well, what piece of pseudocode woould you add to this algorithm?
Explain the purpose of this algorithm.
Which line is an example of a variable updating itself?
Why are item_price and total_price declared as REAL values and not INTEGER?
What piece of pseudocode would you add to include a 10% discount for total price bigger than $500 ? And where would you add it?