B2.1.1 Construct and trace programs using a range of global and local variables of various data types.
• Data types: Boolean value, char, decimal, integer, string
At the heart of every computer program is the management of data. Variables are named containers for this data. They can be global (accessible from anywhere in the program) or local (only accessible within a specific block or function). Each variable also has a data type, which determines what kind of data it can hold—such as whole numbers (integers), text (strings), true/false values (Booleans), individual characters (char), or decimal numbers (floats).
In this standard, we focus on writing and tracing programs that use different data types correctly, and that distinguish between local and global variables. Tracing a program means carefully following it line-by-line to understand the values of variables at each step.
Definitions
- Local Variable: A variable declared inside a function or block. It can only be used within that function or block.
- Global Variable: A variable declared outside any function. It can be used anywhere in the code.
- Data Types:
Boolean: StoresTrueorFalsechar: Stores a single character (e.g.,'A')decimal(float in Python): Stores fractional numbers (e.g.,3.14)integer: Stores whole numbers (e.g.,7)string: Stores text (e.g.,"hello")
We have different data types because:
They save memory (don’t use more than you need).
They tell the computer what operations make sense.
They make programs faster and more efficient.
They make our code easier to read and understand.
Worked Example 1: Scope and Data Types
# Global variables
username = "Alice"
is_logged_in = False # Boolean
def login(user_input):
# Local variables
correct_password = "1234" # string
attempts = 0 # integer
success = False # Boolean
while attempts < 3:
password = input("Enter password: ") # string
if password == correct_password:
success = True
break
else:
attempts += 1
return success
is_logged_in = login("Alice")
Trace Table
| Line | Variable | Scope | Value |
|---|---|---|---|
| 1 | username | Global | "Alice" |
| 2 | is_logged_in | Global | False |
| 4 | correct_password | Local | "1234" |
| 5 | attempts | Local | 0 → 1 → 2... |
| 6 | success | Local | False → True |
| 8 | password | Local | User input |
After execution, is_logged_in is updated based on the return value of login().
Worked Example 2: Using All Data Types
# Global decimal
pi = 3.14159
def circle_area(radius): # radius: decimal (float)
area = pi * radius * radius # area: decimal
print("Area is", area)
def greeting(name): # name: string
initial = name[0] # char
print("Hello", name, "Your name starts with", initial)
def toggle(state): # state: Boolean
return not state
# Calling functions
circle_area(2.5)
greeting("Zoe")
print(toggle(True)) # outputs False
Notes:
piis a global float (decimal).initialis a char (first character of a string).stateand the return value fromtoggle()are Boolean.
A trace table is a structured way to track how the values of variables change during the execution of a program. This helps visualize:
- how the algorithm behaves,
- whether the logic is correct,
- and how many iterations or steps are performed.
Example: Trace Table with Iteration
Let’s trace this simple program:
count = 0
total = 0
while count < 3:
total = total + count
count = count + 1
print("Final total:", total)
Step-by-step trace table
| Step | count | total | count < 3 | Action |
|---|---|---|---|---|
| 1 | 0 | 0 | True | Enter loop |
| 2 | 0 | 0 | total = 0 + 0 = 0 | |
| 3 | 1 | 0 | True | count = 0 + 1 |
| 4 | 1 | 0 | total = 0 + 1 = 1 | |
| 5 | 2 | 1 | True | count = 1 + 1 |
| 6 | 2 | 1 | total = 1 + 2 = 3 | |
| 7 | 3 | 3 | False | Loop ends, print total |
Final output:
Final total: 3
Example with Selection (If-Else)
x = 7
if x % 2 == 0:
parity = "even"
else:
parity = "odd"
| Step | x | x % 2 == 0 | parity |
|---|---|---|---|
| 1 | 7 | False | "odd" |
Summary
By writing and tracing programs that use:
- different data types (Boolean, char, decimal, integer, string),
- and different scopes (local vs global),
…students build precision in program logic and data flow. This is foundational for all higher-level programming tasks. Practice regularly tracing and explaining your own code with trace tables and variable state snapshots.
Please remember, when tracing:
- Focus on variables that change (loop counters, accumulators, flags).
- Capture the state before and after each iteration.
- Use the table to verify correctness or diagnose bugs.