Home Notes Papers

Programming concepts

Paper 2

This section is examined in Paper 2.

Core Programming Constructs
Programming concepts are the fundamental building blocks used to create algorithms. Regardless of the specific programming language, all programs rely on three main control structures: Sequence, Selection, and Iteration. Understanding these allows you to design logic that processes data correctly.
1. Sequence
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 True or False. Used for logical flags and conditions.
Pseudocode

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, and ENDWHILE are standard conventions.
Data Validation vs. Verification
It is critical to distinguish between these two concepts, as they serve different purposes in input processing.
ConceptDefinitionWho/What does it?
ValidationChecks if the data is valid (correct type, range, format) for the specific application.The Program (automated logic).
VerificationChecks 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:

  1. Runtime Errors: The program crashes (e.g., dividing by zero, or trying to add text to a number).
  2. Logical Errors: The program runs but produces incorrect results.
  3. Security Vulnerabilities: Malicious input (like SQL injection) can compromise the system.
Trace Table Execution
A Trace Table is used to manually execute an algorithm step-by-step, recording the value of every variable at each stage. This helps identify logical errors.
Example Algorithm:

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

Key Takeaway: Always show the state of variables after each iteration. Examiners require evidence of at least two iterations and the final termination step to award full marks.
⚠︎ Variable Initialization and Loop Logic
Mistake 1: Uninitialized Variables
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.
Mistake 2: Off-by-One Errors in Loops
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.
Mistake 3: Confusing Validation with Verification
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.
Writing Correct Pseudocode and Output
Tip 1: Punctuation in OUTPUT
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 by OUTPUT Average.
  • Why: The markscheme requires evidence that you know how to manipulate data types. Omitting the ROUND function or the final OUTPUT command 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.
Past Paper Style Questions
Q:
Identify two validation checks that could be applied to a user entering their age.
A:
  1. Range Check: Ensuring the age is between 0 and 120.
  2. Type Check: Ensuring the input is an integer (not text or special characters).
Q:
Explain the difference between a syntax error and a logical error.
A:
A syntax error occurs when the code violates the rules of the programming language (e.g., missing semicolon, misspelled keyword), preventing the program from compiling.
A logical error occurs when the code compiles and runs but produces incorrect results due to flawed algorithm logic.
Q:
Write pseudocode to find the maximum value in an array of 10 numbers.
A:
Max ← Array[1]
FOR i FROM 2 TO 10 DO
IF Array[i] > Max THEN
Max ← Array[i]
ENDIF
ENDFOR
OUTPUT Max
Q:
Why is it important to initialize a counter variable before a loop?
A:
Initialization sets the starting value of the variable. If not initialized, the variable may contain garbage data (random memory values), leading to unpredictable loop behavior or immediate termination if the initial value already meets the exit condition.
Beta v0.7.8 Free while we're in beta — it transitions to paid post launch. Thank you for supporting us at this stage!