B2.1.1 Construct and trace programs using a range of global and local variables of various data types.

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.

 

You should really learn about functions and modularization prior to this learning statement.

 

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: Stores True or False
    • char: 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

LineVariableScopeValue
1usernameGlobal"Alice"
2is_logged_inGlobalFalse
4correct_passwordLocal"1234"
5attemptsLocal0 → 1 → 2...
6successLocalFalse → True
8passwordLocalUser 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:

  • pi is a global float (decimal).
  • initial is a char (first character of a string).
  • state and the return value from toggle() 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

Stepcounttotalcount < 3Action
100TrueEnter loop
200 total = 0 + 0 = 0
310Truecount = 0 + 1
410 total = 0 + 1 = 1
521Truecount = 1 + 1
621 total = 1 + 2 = 3
733FalseLoop ends, print total

Final output:

Final total: 3

Example with Selection (If-Else)

x = 7
if x % 2 == 0:
    parity = "even"
else:
    parity = "odd"
Stepxx % 2 == 0parity
17False"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.