Year 9 Computing Revision Notes

Variables and Assignment

A variable is a named container that stores data that can change while a program is running.


Variable Assignment Syntax
  • Assignment Operator (=): Assigns the value on the right to the variable on the left (e.g., age = 14, name = "Ali", score = 72). Assigning a new value overwrites the existing value.

  • Variable Naming Rules:

    • Use meaningful names (e.g., student_score).

    • May contain letters, numbers, and underscores (_).

    • Must not start with a number.

    • Must not contain spaces or special symbols (such as @, #, !, %, -).

    • Case-sensitive (score and Score are distinct variables).

Data Types and Typecasting

Data types specify the kind of data stored in a program.


Data Types Table
  • Data Types:

    • Integer (int): Positive or negative whole numbers, including zero (e.g., 42, -12, 0).

    • Float (float): Numbers with decimal points (e.g., 1.42, -0.002, 4.0).

    • String (str): Text or characters inside quotation marks (e.g., "Hello", "Ken123").

    • Boolean (bool): Truth values (True, False).

  • User Input:

    • The input() function captures user input and always returns it as a string (str).

  • Typecasting:

    • Typecasting is the process of converting a value from one data type to another.


Typecasting Functions
  • Typecasting Functions:

    • int(): Converts to integer (e.g., int("4") →4\rightarrow 4, int(3.7) →3\rightarrow 3).

    • float(): Converts to floating-point number (e.g., float(3) →3.0\rightarrow 3.0, float("6.7") $I\rightarrow 6.7).\n * `str()`: Converts to string (e.g., `str(4)` \rightarrow \text{"4"}$$).

    • bool(): Converts to boolean. bool(0) and bool("") evaluate to False; any non-zero number or non-empty string evaluates to True.

  • Formatted Strings (f-strings): print(f"My name is {name}") provides formatted outputs embedding variables directly.

Algorithms and Pseudocode

An algorithm is a step-by-step sequence of instructions used to solve a problem, independent of programming languages.


Algorithm Pseudocode Coding Process
  • Pseudocode Conventions:

    • Enclose programs between START and END keywords.

    • Use DISPLAY to output messages or values.

    • Use INPUT() to receive user data.

    • Use ← for variable assignment (e.g., score ← 50).

    • Group nested instructions for structures inside { } curly brackets.

Selection and Logical Operators

Conditional statements control program flow by making decisions based on truth conditions.

  • Comparison Operators: =, >, <, >=, <=, != (Python uses == for equality comparison).

  • Logical Operators:

    • AND: Evaluates to True only when both conditions are true.

    • OR: Evaluates to True when at least one condition is true.

    • NOT: Inverts the truth value of a condition.

  • Conditional Structures: Evaluated in sequence using IF, ELSE IF, and ELSE. Processing halts for the statement block once a condition evaluates to True.

Loops and Iteration

Loops repeatedly execute a block of instructions.

  • Count-Controlled Loops:

    • Executed when the exact number of iterations is known in advance.

    • Pseudocode: REPEAT n TIMES { ... }

    • Python: for i in range(start, stop): (e.g., range(1, limit + 1) iterates up to limit).

  • Condition-Controlled Loops:

    • Executed when iterations depend on an unknown number of repetitions until a condition is met.

    • Pseudocode: REPEAT UNTIL (condition) { ... }

    • Python: while loop.

  • Modulo Operator (MOD): Calculates division remainders (e.g., i MOD 2 = 0 determines if i is an even number).

Code Tracing

Tracing is a manual step-by-step execution of an algorithm to track and record variable modifications across iterations.


Factorial Trace Table
  • Tracing Rules:

    • Maintain initial variable values at start.

    • Evaluate expressions using old right-hand variable values before assigning the updated result to left-hand variables.

    • Record state changes systematically row-by-row in a trace table for every iteration.