Programming concepts
This is the default flow of execution. Instructions are carried out one after another, in the exact order they are written. There are no jumps or decisions; the computer simply moves from line 1 to line 2, then line 3, and so on.
2. Selection (Conditional Logic)
Selection allows the program to make decisions based on conditions. It uses Boolean logic (True/False) to determine which path of code to execute.
- IF...THEN...ELSE: Executes a block of code if a condition is True; otherwise, executes a different block.
- CASE / SWITCH: Used when there are multiple distinct options based on the value of a single variable. It is more efficient than multiple IF statements for discrete values.
3. Iteration (Loops)
Iteration repeats a block of code. There are two main types:
- Count-controlled loops (FOR loops): Used when the number of repetitions is known beforehand. The loop runs for a specific count (e.g.,
FOR i FROM 1 TO 10). - Condition-controlled loops (WHILE/REPEAT-UNTIL): Used when the number of repetitions is unknown and depends on a condition.
- Pre-condition loop (WHILE): Checks the condition before executing the body. If the condition is initially False, the body never runs.
- Post-condition loop (REPEAT-UNTIL): Executes the body at least once, then checks the condition. It repeats until the condition becomes True.
4. Variables and Data Types
A variable is a named storage location in memory that holds a value.
- Integer: Whole numbers (e.g.,
5,-10). Used for counting or discrete quantities. - Float/Real: Numbers with decimal points (e.g.,
3.14,9.8). Used for calculations requiring precision. - String: A sequence of characters enclosed in quotes (e.g.,
'Hello'). Used for text. - Boolean: Can only be
TrueorFalse. Used for logical flags and conditions.
Pseudocode is a simplified, high-level description of a computer program or algorithm. It uses the structural conventions of programming but is intended for human reading rather than machine execution.
- Purpose: To plan logic without worrying about specific syntax errors of a language like Python or Java.
- Key Rule: Pseudocode must be unambiguous. Keywords like
←(assignment),OUTPUT,INPUT,IF,WHILE, andENDWHILEare standard conventions.
| Concept | Definition | Who/What does it? |
|---|---|---|
| Validation | Checks if the data is valid (correct type, range, format) for the specific application. | The Program (automated logic). |
| Verification | Checks if the data entered matches what was intended or expected by the user. | The User (manual action) or a simple program check. |
Why Validation is Necessary:
Without validation, 'Garbage In, Garbage Out' occurs. Invalid data can cause:
- Runtime Errors: The program crashes (e.g., dividing by zero, or trying to add text to a number).
- Logical Errors: The program runs but produces incorrect results.
- Security Vulnerabilities: Malicious input (like SQL injection) can compromise the system.
Total ← 0
Counter ← 1
WHILE Counter ≤ 3 DO
Total ← Total + Counter
Counter ← Counter + 1
ENDWHILE
OUTPUT Total
Trace Table:
| Iteration | Counter (start) | Condition (Counter ≤ 3) | Total Calculation | Total (end) |
|---|---|---|---|---|
| 1 | 1 | True | 0 + 1 | 1 |
| 2 | 2 | True | 1 + 2 | 3 |
| 3 | 3 | True | 3 + 3 | 6 |
| 4 | 4 | False | Loop terminates | 6 |
Final Output: 6
Error: Assuming a variable like
Total starts at 0 automatically.Correction: You must explicitly initialize accumulators and counters. E.g.,
Total ← 0 before the loop. If you don't, the result is unpredictable or incorrect.Error: Using
< instead of ≤ (or vice versa) in a count-controlled loop, causing the loop to run one time too many or too few.Correction: Carefully check the boundary condition. If you need to process items 1 through 10, use
WHILE i ≤ 10.Error: Describing a validation check (e.g., 'Range Check') as a verification method.
Correction: Remember: Validation is automated program logic checking data integrity. Verification is often a user action (like re-typing a password) or a simple format check.
When asked to output text, you must use quotation marks. Examiners mark down answers like
OUTPUT Hello because it looks like a variable name. The correct format is OUTPUT 'Hello'. This explicitly tells the compiler/reader that 'Hello' is a string literal, not a variable.Tip 2: Rounding in Algorithms
If an algorithm requires an average or rounded number, you must explicitly include the rounding step. Do not assume the output will be rounded automatically.
- Correct:
Average ← ROUND(Total / Count, 0)followed byOUTPUT Average. - Why: The markscheme requires evidence that you know how to manipulate data types. Omitting the
ROUNDfunction or the finalOUTPUTcommand results in lost marks.
Tip 3: Describing Procedures/Functions
When asked why a programmer would use a procedure, do not just define what it is. Explain the benefit.
- Acceptable Phrase: 'It promotes reusability, allowing the code to be called multiple times without rewriting.'
- Why: This directly addresses the syllabus requirement for modular programming benefits.
- Range Check: Ensuring the age is between 0 and 120.
- Type Check: Ensuring the input is an integer (not text or special characters).
A logical error occurs when the code compiles and runs but produces incorrect results due to flawed algorithm logic.
Max ← Array[1]FOR i FROM 2 TO 10 DO IF Array[i] > Max THEN Max ← Array[i] ENDIFENDFOROUTPUT Max