Introduction to AQA Standard Pseudo-code
Welcome! If you have ever tried to follow a recipe to bake a cake, you have used an algorithm. In Computer Science, we need a way to write down these "recipes" so that anyone can understand them, regardless of which programming language they use (like Python or C#). This is where pseudo-code comes in!
Pseudo-code is a way of writing out an algorithm using English-like words. It isn't a "real" programming language that a computer can run, but it follows strict rules so it is clear and unambiguous. For your AQA exams, the questions will use a specific "Standard Pseudo-code." Learning these rules will help you read exam questions easily and write your own algorithms like a pro.
1. The Basics: Assignment, Constants, and Comments
Before we build complex logic, we need to know how to store data and leave notes for ourselves.
- Comments: Use the \(#\) symbol. Anything after this on the same line is ignored by the "computer." It’s just for humans!
- Assignment: To store a value in a variable, we use the left-pointing arrow \(\leftarrow\).
Example: \(score \leftarrow 10\) (This means "put the value 10 into the variable called score"). - Constants: These are values that never change while the program is running. We use the CONSTANT keyword and write the name in CAPITAL LETTERS.
Example: \(CONSTANT\ PI \leftarrow 3.14\)
Quick Tip: Always use meaningful identifier names. Instead of calling a variable \(x\), call it \(userAge\). It makes your code much easier to read!
2. Input and Output
Algorithms need to talk to the user. We use two main commands for this:
OUTPUT: Displays information on the screen. You can output strings (text) or variables.
Example: \(OUTPUT\ 'Hello\ ', name\) (This prints "Hello" followed by whatever is stored in the \(name\) variable).
USERINPUT: This waits for the user to type something and then returns that value.
Example: \(userAge \leftarrow USERINPUT\)
3. Arithmetic Operations
AQA pseudo-code uses standard math symbols, but there are two special ones you must remember for division:
- \(+\) (Addition)
- \(-\) (Subtraction)
- \(*\) (Multiplication)
- \(/\) (Real Division): This gives the full answer, e.g., \(7 / 2 = 3.5\).
- DIV (Integer Division): This tells you how many whole times a number goes into another.
Example: \(7\ DIV\ 2 = 3\) - MOD (Modulus): This gives you the remainder left over after division.
Example: \(7\ MOD\ 2 = 1\) (because 2 goes into 7 three times, with 1 left over).
Don't worry if this seems tricky! Just remember: DIV is the whole number, MOD is the leftover bit.
4. Selection (Making Decisions)
Sometimes an algorithm needs to take different paths depending on a condition. We use IF statements for this.
Relational Operators:
To compare values, we use: \(=\) (equal to), \(\neq\) (not equal to), \(<\) (less than), \(>\) (greater than), \(\leq\) (less than or equal to), and \(\geq\) (greater than or equal to).
Structure:
IF userAge \(\geq\) 18 THENOUTPUT 'You are an adult'
ELSE IF userAge \(\geq\) 13 THEN
OUTPUT 'You are a teenager'
ELSE
OUTPUT 'You are a child'
ENDIF
Key Takeaway: Every IF must eventually have an ENDIF to show where the decision-making finishes.
5. Iteration (Loops)
Computers are great at doing things over and over again! AQA uses three main types of loops:
A. WHILE Loops (Condition-controlled)
The condition is checked before the loop starts. If the condition is False to begin with, the code inside might never run.
WHILE answer \(\neq\) 'correct'OUTPUT 'Try again'
answer \(\leftarrow\) USERINPUT
ENDWHILE
B. REPEAT...UNTIL Loops (Condition-controlled)
The condition is checked at the end. This means the code inside always runs at least once.
REPEATOUTPUT 'Enter a positive number'
num \(\leftarrow\) USERINPUT
UNTIL num \(>\) 0
C. FOR Loops (Count-controlled)
Use these when you know exactly how many times you want to repeat something.
FOR i \(\leftarrow\) 1 TO 5OUTPUT i
ENDFOR
This would print: 1, 2, 3, 4, 5. You can also add a STEP (like \(STEP\ 2\) to count in twos).
6. Data Structures: Arrays and Records
Arrays: Used to store multiple items of the same data type in one list.
Important: AQA arrays start counting at index 0.
- Create: \(names \leftarrow ['Alice', 'Bob', 'Charlie']\)
- Access: \(names[0]\) would be 'Alice'.
- Update: \(names[1] \leftarrow 'Billy'\)
- Length: \(LEN(names)\) would give you 3.
Records: Used to store related data of different types (like a "row" in a database).
RECORD Studentname : String
age : Integer
ENDRECORD
7. Subroutines (Procedures and Functions)
Subroutines are mini-programs inside your main program. They help break down big problems (this is called decomposition).
- Procedure: Just carries out a task.
- Function: Carries out a task AND returns a value back to the main program.
Example of a Function:
SUBROUTINE calculateDouble(number)result \(\leftarrow\) number * 2
RETURN result
ENDSUBROUTINE
Did you know? Using local variables inside subroutines is good practice because they only exist while the subroutine is running, which saves memory and prevents accidental changes to other parts of your code!
8. String Handling
Strings are just sequences of characters. AQA expect you to know these commands:
- LEN(string): Returns the number of characters.
- POSITION(string, char): Finds the index of a character (starting at 0).
- SUBSTRING(start, end, string): Pulls out a section of the string from the start position to the end position.
- Concatenation (+): Joining two strings together. \( 'Rob' + 'ot' \) becomes \( 'Robot' \).
Quick Summary Checklist
Before your exam, make sure you can identify and use:
- \(\leftarrow\) for assignment.
- DIV for whole numbers and MOD for remainders.
- IF...THEN...ELSE...ENDIF for selection.
- WHILE, REPEAT...UNTIL, and FOR for loops.
- The fact that arrays start at index 0.
- USERINPUT and OUTPUT for communicating.
Common Mistake to Avoid: Forgetting to use ENDIF, ENDWHILE, or ENDFOR. In AQA pseudo-code, you must always "close" your blocks of code!