Twa kɔ nsɛm atitiriw so
Log in
Sign up for FREE
arrow_back
Laabri

IGCSE Topic 7: Variables and Constants

star
star
star
star
star
Last updated 4 months ago
34 Nsɛmmisa

1. The Core Concept

Start with the absolute basics. Do not use "boxes" or "buckets." Use the technical definition immediately but keep it simple.

  • Variable: A named storage location in memory that holds a value.

  • Assignment: The act of storing a value in a variable. It overwrites whatever was there before.

  • Syntax: We will use name <- value .

The Golden Rule: Assignment always works from Right to Left. The computer calculates the value on the right side first, then saves it into the variable name on the left.

Step 1: Creation and Initialization

Goal: Understand that a name holds a specific value.

Look at the following piece of Python code:

score = 10

name = "Alex"

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
1.

What value is stored in the variable score ?

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
2.

What value is stored in the variable name ?

Step 2: Overwriting (Destructive Assignment)

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)

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
3.

Which value will be displayed on the screen after the last line of code is executed?

Step 3: Using Variables to Assign Variables

Goal: Understand looking up a value.

Look at the following piece of Python code:

x = 5

y = x

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
4.

What is the value assigned to the variable x ?

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
5.

What value is stored in the variable y ?

Ɛhia
1
Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
7.

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?

Step 4: The Self-Reference (The Increment)

Goal: Master the Right-to-Left logic.

Look at the following piece of Python code:

points = 10

points = points + 1

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
8.

Explain what happens in the second line.

Ɛhia
1
Asemmisa {{asɛmmisaAhyɛnsode}}
9.

If print points were added to the code on the left, what would be displayed on the screen?

Step 5: Trace Table Challenge

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

Ɛhia
4
Asemmisa {{asɛmmisaAhyɛnsode}}
10.

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

2. Common Logic Traps to Fix Immediately

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.

Ɛhia
1
Ɛhia
1
Ɛhia
1
Ɛhia
1
Ɛhia
4
Ɛhia
5
Ɛhia
10
Ɛhia
4
Ɛhia
1
Ɛhia
1
Ɛhia
1
Ɛhia
1
Ɛhia
1
Ɛhia
1
Ɛhia
2
Ɛhia
1
Ɛhia
4

4. Variables updating themselves

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

Ɛhia
3
Ɛhia
1
Ɛhia
2
Ɛhia
4

UNDERSTANDING & CHALLENGE:

The Broken Code:

area = width * height

width = 5

height = 10

print(area)

Ɛhia
3
Asemmisa {{asɛmmisaAhyɛnsode}}
32.

Why will this code likely crash or print "0" (or null)?

UNDERSTANDING & CHALLENGE:

Scenario: The student wants to swap 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)

Ɛhia
2
Asemmisa {{asɛmmisaAhyɛnsode}}
33.

If you run this, both a and b end up as 3. Why didn't they swap?

Ɛhia
2
Asemmisa {{asɛmmisaAhyɛnsode}}
34.

How do you fix it?

Asemmisa {{asɛmmisaAhyɛnsode}}
6.

This line ( x = 12) is added to the piece of code on the left. Will it change the value stored in variable y ?

3. Declaring Variables and Defining their Data Types

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.

Asemmisa {{asɛmmisaAhyɛnsode}}
11.

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.

Asemmisa {{asɛmmisaAhyɛnsode}}
12.

Send a message to the user and ask them to enter their date of birth in DD/MM/YY format.

Asemmisa {{asɛmmisaAhyɛnsode}}
13.

Use the DoB variable to collect the user data from the keyboard.

Asemmisa {{asɛmmisaAhyɛnsode}}
14.

Display the following message on the screen:

"You entered <display their date of birth here> as you date of birth. Recorded."

Asemmisa {{asɛmmisaAhyɛnsode}}
15.

The following algorithm is designed to calcuate the area of a circle. Put the psedocode lines in the corrent order.

  1. area <- r * r * 3.14

  2. INPUT r

  3. OUTPUT "Enter the radius of the circle: "

  4. DECLARE area , r : REAL

  5. OUTPUT "The area of the circle is: ", area

Asemmisa {{asɛmmisaAhyɛnsode}}
16.

The following algorithm is designed to calcuate a final price. Put the psedocode lines in the corrent order.

  1. DECLARE units : INTEGER

  2. INPUT units

  3. OUTPUT "Enter the number of of notebooks you would like to purchase: "

  4. unit_price <- 2.55

  5. discount <- 0.8

  6. DECLARE discount : REAL

  7. total_price <- unit_price * units

  8. DECLARE unit_price , total_price : REAL

  9. IF units > = 20 THEN total_price <- total_price * discount ENDIF

  10. OUTPUT "Total to be paid: ", total_price

Asemmisa {{asɛmmisaAhyɛnsode}}
17.
Mmuae Afoforo a Wobɛpaw:

OUTPUT

'N'

basket

INPUT

DECLARE

continue

item

CHAR

Asemmisa {{asɛmmisaAhyɛnsode}}
18.

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.

4. Variables updating themselves

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!"

Asemmisa {{asɛmmisaAhyɛnsode}}
19.

What name would you choose for the algorithm given?

Asemmisa {{asɛmmisaAhyɛnsode}}
20.

Which line controls the number of iterations of the loop?

Asemmisa {{asɛmmisaAhyɛnsode}}
21.

Which line is an example of a variable updating itself?

Asemmisa {{asɛmmisaAhyɛnsode}}
22.

Which code line is responsible for incrementing the variable loop_counter?

Asemmisa {{asɛmmisaAhyɛnsode}}
23.
Asemmisa {{asɛmmisaAhyɛnsode}}
24.

How many numbers will the user have the chance to enter as input?

Asemmisa {{asɛmmisaAhyɛnsode}}
25.

What purpose do lines 08-10 serve in this algorithm?

Asemmisa {{asɛmmisaAhyɛnsode}}
26.

Which single line is responsible for keeping a record of positive numbers?

Asemmisa {{asɛmmisaAhyɛnsode}}
27.

If you wanted to count the negative numbers as well, what piece of pseudocode woould you add to this algorithm?

Asemmisa {{asɛmmisaAhyɛnsode}}
28.

Explain the purpose of this algorithm.

Asemmisa {{asɛmmisaAhyɛnsode}}
29.

Which line is an example of a variable updating itself?

Asemmisa {{asɛmmisaAhyɛnsode}}
30.

Why are item_price and total_price declared as REAL values and not INTEGER?

Asemmisa {{asɛmmisaAhyɛnsode}}
31.

What piece of pseudocode would you add to include a 10% discount for total price bigger than $500 ? And where would you add it?