Variables, Constants, Operators & Library Routines
9618 AS Computer Science
Variables are called identifiers because they identify a storage location in memory. When you create a variable named "Age", the computer reserves a specific memory location and uses "Age" as a label to find it quickly!
A variable is an identifier that can change in the lifetime of a program. It is a container for storing data values with a name that can change during the execution of a program.
In programming, we need to DECLARE variables (also called identifiers). When a variable is declared, memory is allocated based on the data type indicated.
To declare a variable, use the keyword DECLARE followed by the name and data type:
You can assign a value using the assignment operator ←:
A constant is an identifier set once in the lifetime of a program. It is a location in memory where data is stored that cannot be changed while the program is running.
To declare a constant, use the keyword CONSTANT:
| Feature | Variable | Constant |
|---|---|---|
| Value Change | Can change during program execution | Cannot change once set |
| Naming Convention | Pascal case (e.g., TotalScore) | UPPERCASE (e.g., MAX_SCORE) |
| Declaration Keyword | DECLARE | CONSTANT |
| Memory Allocation | Allocated when declared | Allocated when declared |
| Use Case | Storing changing data (counters, inputs) | Fixed values (π, max values, settings) |
| Feature | Python | VB.NET | Java |
|---|---|---|---|
| Declare variable | Just assign it (no keyword needed) | Dim radius As Double | double radius; |
| Assign variable | radius = 5 | radius = 5 | radius = 5; |
| Declare constant | PI = 3.14159 (convention: UPPERCASE) | Const Pi As Double = 3.14159 | final double PI = 3.14159; |
| Reassign constant? | Yes (not truly constant unless enforced) | Cannot change once set | Cannot change once set |
| Use in calculation | area = PI * radius * radius | area = Pi * radius * radius | area = PI * radius * radius; |
In pseudocode exams, always use DECLARE for variables and CONSTANT for constants with proper data types. Python conventions don't apply to pseudocode!
Many Values to Multiple Variables:
One Value to Multiple Variables:
Unpack a Collection:
Arithmetic operators are used to perform basic maths operations in a program. These include adding, subtracting, multiplying and dividing values.
| Operator | Purpose | Example | Result |
|---|---|---|---|
| + | Addition or string concatenation | 5 + 3 "John" + " " + "Doe" |
8 "John Doe" |
| - | Subtraction | 10 - 4 | 6 |
| * | Multiplication | 3 * 5 | 15 |
| / | Division | 10 / 2 | 5 |
| ^ | Exponentiation (power of) | 3 ^ 3 | 27 |
| MOD | Modulus (remainder after division) | 10 MOD 3 | 1 |
Arithmetic operators follow operator precedence. Multiplication and division happen before addition and subtraction unless brackets are used.
Think of MOD as "What's left over?" When you divide 10 by 3, you get 3 with remainder 1. That remainder (1) is what MOD gives you!
10 MOD 3 = 1 (because 10 = 3 × 3 + 1)
Logical operators (also called comparison operators) are used to compare values. They return either TRUE or FALSE and are commonly used in conditions and loops.
| Operator | Purpose | Example | Result |
|---|---|---|---|
| = | Equal to | 5 = 6 | FALSE |
| <> | Not equal to | 5 <> 7 | TRUE |
| > | Greater than | 5 > 10 | FALSE |
| < | Less than | 5 < 10 | TRUE |
| >= | Greater than or equal to | 5 >= 10 | FALSE |
| <= | Less than or equal to | 5 <= 10 | TRUE |
In pseudocode, use = for equality comparison (not == like in Python/Java). Use <> for "not equal to" (not != like in many programming languages).
Don't confuse = (comparison/equality) with ← (assignment). In pseudocode:
Global variables are variables that are created outside of a function. They can be used by everyone, both inside of functions and outside.
If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
Normally, when you create a variable inside a function, that variable is local. To create a global variable inside a function, you can use the global keyword.
Also, use the global keyword if you want to change a global variable inside a function.
It is good practice to declare (set up) your variables at the start of a program. The reason for this is that each time you declare a variable, a part of the computer's memory is reserved for it. Declaring these variables at the start allows the computer to calculate whether or not it actually has enough memory to run the program.
A library routine is a debugged block of code (subroutine, procedure, function, etc.), often designed to handle commonly occurring problems or tasks.
Libraries are a collection of ready-made sub-routines that can be called by programs executing within the host operating system. Library routines are stored in a program library and given names. This allows them to be called into immediate use when needed, even from other programs.
| Benefit | Explanation |
|---|---|
| Faster Development | Makes writing programs faster as part of the work has already been done |
| Already Debugged | Library routines are tested and debugged, reducing errors in your code |
| Reusability | Same routine can be used by multiple programs |
| Saves Time | No need to reinvent the wheel - common tasks already solved |
| Consistency | Standard routines ensure consistent behavior across programs |
When asked about library routines, mention: (1) They are pre-written and debugged code, (2) They save development time, (3) They can be reused by multiple programs, (4) Examples include math functions, file handling, and UI routines.
When a variable is declared, memory is allocated based on the data type indicated. Different data types require different amounts of memory and can store different kinds of values.
| Data Type | Description | Example Values |
|---|---|---|
| INTEGER | Whole numbers (no decimals) | 5, -3, 0, 100 |
| REAL | Numbers with decimal places | 3.14, -2.5, 0.001 |
| STRING | Text characters (letters, numbers, symbols) | "Hello", "123", "A" |
| BOOLEAN | Logical values (true or false only) | TRUE, FALSE |
| DATE | Date values | 01/01/2025, 15/06/2024 |
| CHAR | Single character | 'A', '5', '!' |
Answer:
Additional points for deeper understanding:
Answer:
Marks awarded for:
Answer:
a) 3 + 4 * 2 = 3 + 8 = 11
(Multiplication before addition)
b) (3 + 4) * 2 = 7 * 2 = 14
(Brackets evaluated first)
c) 10 MOD 3 = 1
(10 ÷ 3 = 3 remainder 1)
d) 2 ^ 3 + 1 = 8 + 1 = 9
(Exponentiation before addition)
Answer:
Naming conventions:
Importance:
Answer:
a) x = y → 7 = 3 → FALSE
b) x <> y → 7 <> 3 → TRUE
c) x > y → 7 > 3 → TRUE
d) x <= 7 → 7 <= 7 → TRUE
Additional points for deeper understanding:
Answer:
A library routine is a debugged block of code (subroutine, procedure, or function) designed to handle commonly occurring problems or tasks. It is stored in a program library and can be called when needed.
Benefits:
Answer:
Marks awarded for:
Answer:
= is the equality comparison operator. It compares two values and returns TRUE if they are equal, FALSE otherwise.
← is the assignment operator. It assigns a value to a variable.
Examples:
Additional note: In pseudocode, use = for comparison (not ==) and ← for assignment.
Answer:
Global variables are declared outside of functions and can be accessed anywhere in the program.
Local variables are declared inside functions and can only be accessed within that function.
Using the global keyword:
Answer:
Marks awarded for:
| Term | Definition |
|---|---|
| Variable | An identifier that can change in the lifetime of a program; a named storage location in memory |
| Constant | An identifier set once in the lifetime of a program; its value cannot be changed during execution |
| Identifier | The name given to a variable or constant; used to refer to a storage location in memory |
| Declaration | The process of creating a variable or constant, specifying its name and data type |
| Assignment | The process of giving a value to a variable using the ← operator |
| Data Type | A classification that specifies what type of value a variable can hold (e.g., INTEGER, STRING) |
| Pascal Case | Naming convention where each word starts with a capital letter with no spaces (e.g., FirstName) |
| Arithmetic Operator | Symbols used to perform mathematical operations (+, -, *, /, ^, MOD) |
| Logical Operator | Symbols used to compare values, returning TRUE or FALSE (=, <>, <, >, <=, >=) |
| Operator Precedence | The rules that determine the order in which operations are evaluated (BODMAS) |
| MOD | Modulus operator; returns the remainder after division of one number by another |
| Library Routine | A debugged block of code designed to handle commonly occurring tasks, stored in a library |
| Global Variable | A variable declared outside a function, accessible from anywhere in the program |
| Local Variable | A variable declared inside a function, accessible only within that function |
| Scope | The region of code where a variable is accessible (global or local scope) |
| Exponentiation | Raising a number to a power; indicated by the ^ operator (e.g., 3^2 = 9) |
| Concatenation | Joining strings together using the + operator (e.g., "Hello" + "World") |
Always include the data type when declaring variables:
Common data types: INTEGER, REAL, STRING, BOOLEAN, DATE, CHAR
Always remember the order:
When in doubt, use brackets to make the order clear!
Always apply operations in this order:
When in doubt, use brackets to make the order clear!
Think of division in two parts:
Example: 17 ÷ 5 = 3 remainder 2
So: 17 DIV 5 = 3 and 17 MOD 5 = 2
When asked to write pseudocode, include ALL parts:
Remember: Pseudocode has different syntax from Python, Java, or VB.NET:
| Feature | Pseudocode | Python | Java |
|---|---|---|---|
| Assignment | ← | = | = |
| Equality | = | == | == |
| Not equal | <> | != | != |
| Declare variable | DECLARE x : INTEGER | x = 0 | int x; |