Preskoči na glavni sadržaj
Prijava
Sign up for FREE
arrow_back
Biblioteka

IGCSE Topic 7: Variables and Constants

star
star
star
star
star
Posljednje ažuriranje 4 months ago
34 questions

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"

Obavezno
1
Pitanje 1
1.

What value is stored in the variable score ?

Obavezno
1
Pitanje 2
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)

Obavezno
1
Pitanje 3
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

Obavezno
1
Pitanje 4
4.

What is the value assigned to the variable x ?

Obavezno
1
Pitanje 5
5.

What value is stored in the variable y ?

Obavezno
1
Obavezno
1
Pitanje 7
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

Obavezno
1
Pitanje 8
8.

Explain what happens in the second line.

Obavezno
1
Pitanje 9
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

Obavezno
4
Pitanje 10
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.

Obavezno
1
Obavezno
1
Obavezno
1
Obavezno
1
Obavezno
4
Obavezno
5
Obavezno
10
Obavezno
4
Obavezno
1
Obavezno
1
Obavezno
1
Obavezno
1
Obavezno
1
Obavezno
1
Obavezno
2
Obavezno
1
Obavezno
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

Obavezno
3
Obavezno
1
Obavezno
2
Obavezno
4

UNDERSTANDING & CHALLENGE:

The Broken Code:

area = width * height

width = 5

height = 10

print(area)

Obavezno
3
Pitanje 32
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)

Obavezno
2
Pitanje 33
33.

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

Obavezno
2
Pitanje 34
34.

How do you fix it?

Pitanje 6
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.

Pitanje 11
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.

Pitanje 12
12.

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

Pitanje 13
13.

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

Pitanje 14
14.

Display the following message on the screen:

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

Pitanje 15
15.

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

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

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

  3. INPUT r

  4. area <- r * r * 3.14

  5. DECLARE area , r : REAL

Pitanje 16
16.

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

  1. DECLARE unit_price , total_price : REAL

  2. INPUT units

  3. DECLARE units : INTEGER

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

  5. unit_price <- 2.55

  6. total_price <- unit_price * units

  7. DECLARE discount : REAL

  8. discount <- 0.8

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

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

Pitanje 17
17.
Drugi mogući odgovor:

OUTPUT

'N'

basket

INPUT

DECLARE

continue

item

CHAR

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

Pitanje 19
19.

What name would you choose for the algorithm given?

Pitanje 20
20.

Which line controls the number of iterations of the loop?

Pitanje 21
21.

Which line is an example of a variable updating itself?

Pitanje 22
22.

Which code line is responsible for incrementing the variable loop_counter?

Pitanje 23
23.
Pitanje 24
24.

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

Pitanje 25
25.

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

Pitanje 26
26.

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

Pitanje 27
27.

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

Pitanje 28
28.

Explain the purpose of this algorithm.

Pitanje 29
29.

Which line is an example of a variable updating itself?

Pitanje 30
30.

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

Pitanje 31
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?