1. Overview
This section covers the fundamental building blocks of computer programming. A strong understanding of these concepts, such as variables, data types, control structures, and modular programming, is crucial for writing effective and efficient programs. These concepts are the foundation for all further programming topics and problem-solving with code.
Key Definitions
- Algorithm: A step-by-step set of instructions to solve a problem.
- Variable: A named memory location that holds a value which can change during program execution.
- Constant: A named memory location that holds a value which cannot be changed during program execution.
- Data Type: The kind of data that a variable or constant can hold (e.g., integer, string, Boolean).
- Input: Data received by the program, typically from the user.
- Output: Data displayed by the program, typically to the user.
- Sequence: Instructions executed in the order they appear in the program.
- Selection: Choosing which instructions to execute based on a condition.
- Iteration: Repeating a block of instructions multiple times.
- Procedure: A named block of code that performs a specific task and does not return a value.
- Function: A named block of code that performs a specific task and returns a value.
- Parameter: A value passed to a procedure or function.
- Local Variable: A variable declared inside a procedure or function, only accessible within that module.
- Global Variable: A variable declared outside all modules, accessible throughout the program.
Core Content
Variables and Constants:
- Variables:
- Store data that can change.
- Must be declared with a name and data type.
- Example:
DECLARE age : INTEGER - Assigned values using the assignment operator (e.g.,
age <- 16).
- Constants:
- Store data that cannot change after declaration.
- Must be declared with a name, data type and initial value.
- Example:
CONSTANT PI : REAL = 3.14159 - Using meaningful names makes the code easier to understand.
- Variables:
Data Types:
- INTEGER: Whole numbers (e.g., -2, 0, 100).
- REAL: Decimal numbers (e.g., -1.5, 3.14, 0.0).
- CHAR: Single characters (e.g., 'A', '5', '$').
- STRING: Sequences of characters (e.g., "Hello", "IGCSE", "123 Main St").
- BOOLEAN: True or False values.
Choosing the correct data type is essential for efficient memory usage and preventing errors.
Input and Output:
- Input: Used to get data from the user.
- Pseudocode:
INPUT nameorname <- INPUT - Prompt the user for input to ensure they know what to enter.
- Pseudocode:
- Output: Used to display data to the user.
- Pseudocode:
OUTPUT "Hello, ", name - Combine text and variables in the output for clarity.
- Pseudocode:
Example:
OUTPUT "Enter your name: " INPUT name OUTPUT "Hello, ", name- Input: Used to get data from the user.
Sequence:
- Instructions are executed one after another in the order they appear in the code.
- The order of instructions matters – changing the order can change the result.
Example:
num1 <- 10 num2 <- 5 sum <- num1 + num2 OUTPUT sum // Outputs 15Selection:
IF-THEN-ELSE: Executes different blocks of code based on a condition.
IF age >= 18 THEN OUTPUT "You are an adult." ELSE OUTPUT "You are a minor." ENDIFNested IF Statements: IF statements within IF statements.
IF score >= 70 THEN IF score >= 90 THEN OUTPUT "Excellent" ELSE OUTPUT "Good" ENDIF ELSE OUTPUT "Needs improvement" ENDIFCASE/SWITCH: Selects one block of code to execute from several options based on the value of a variable.
CASE OF grade: "A": OUTPUT "Excellent" "B": OUTPUT "Good" "C": OUTPUT "Average" OTHERWISE: OUTPUT "Needs Improvement" ENDCASE
Iteration:
- Count-Controlled Loop (FOR): Repeats a block of code a known number of times.
FOR i <- 1 TO 10 OUTPUT i NEXT i - Pre-Condition Loop (WHILE): Checks a condition before each iteration; the loop may not execute at all.
num <- 1 WHILE num <= 10 DO OUTPUT num num <- num + 1 ENDWHILE - Post-Condition Loop (REPEAT-UNTIL): Checks a condition after each iteration; the loop always executes at least once.
num <- 1 REPEAT OUTPUT num num <- num + 1 UNTIL num > 10
- Count-Controlled Loop (FOR): Repeats a block of code a known number of times.
Totalling and Counting:
- Totalling: Accumulating the sum of values.
total <- 0 FOR i <- 1 TO 5 INPUT value total <- total + value NEXT i OUTPUT "Total: ", total - Counting: Tracking the number of items that meet a specific criteria.
count <- 0 FOR i <- 1 TO 10 INPUT age IF age >= 18 THEN count <- count + 1 ENDIF NEXT i OUTPUT "Number of adults: ", count
- Totalling: Accumulating the sum of values.
String Handling:
LENGTH(string): Returns the number of characters in a string.SUBSTRING(string, start, length): Extracts a substring from a string, starting at thestartposition with the specifiedlength. The first position is index 1.UPPER(string): Converts a string to uppercase.LOWER(string): Converts a string to lowercase.- String Concatenation: Joining strings together, usually using the
&operator.firstName <- "John" lastName <- "Doe" fullName <- firstName & " " & lastName // fullName will be "John Doe" OUTPUT fullName
Arithmetic, Logical, and Boolean Operators:
- Arithmetic:
+(addition),-(subtraction),*(multiplication),/(division),DIV(integer division),MOD(modulus/remainder),^(exponentiation). - Logical/Comparison:
=(equal to),<>(not equal to),<(less than),>(greater than),<=(less than or equal to),>=(greater than or equal to). - Boolean:
AND(both conditions must be true),OR(at least one condition must be true),NOT(reverses the condition).
Operator precedence (order of operations): Brackets, Exponentiation, Multiplication/Division, Addition/Subtraction.
- Arithmetic:
Nested Statements:
- One control structure (IF, FOR, WHILE, REPEAT) placed inside another.
- Example: Nested loops (a loop inside another loop).
This would output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3FOR i <- 1 TO 3 FOR j <- 1 TO 3 OUTPUT i, j NEXT j NEXT i
Procedures, Functions, and Parameters:
Procedure: A block of code that performs a task. It's called by name and does not return a value.
PROCEDURE greet(name) OUTPUT "Hello, ", name ENDPROCEDURE CALL greet("Alice")Function: A block of code that performs a task and returns a value.
FUNCTION add(num1, num2) RETURNS INTEGER sum <- num1 + num2 RETURN sum ENDFUNCTION result <- add(5, 3) OUTPUT result // Outputs 8Parameters: Values passed to procedures or functions. Allow for reusability of the code.
Parameters can be passed by value (a copy is used) or by reference (the original variable is used).
Local and Global Variables:
- Local Variables: Declared within a procedure or function. Only accessible within that module. Created when the module is called, destroyed when it ends.
- Global Variables: Declared outside all modules. Accessible throughout the entire program. Exist for the program's lifetime.
Using local variables is generally better for code organization and preventing unintended side effects. Pass values as parameters to functions/procedures.
Library Routines:
MOD(a, b): Returns the remainder whenais divided byb. Example:MOD(17, 5)returns 2.DIV(a, b): Returns the integer part of the division whenais divided byb. Example:DIV(17, 5)returns 3.ROUND(number, places): Rounds a number to the specified number of decimal places. Example:ROUND(3.14159, 2)returns 3.14.RANDOM(): Generates a random number (usually between 0 and 1, or within a specified range depending on the environment).
Creating a Maintainable Program:
- Meaningful Identifiers: Use descriptive names for variables, constants, procedures, and functions. For example,
totalScoreis better thanx. Use a consistent style (camelCase or snake_case). - Comments: Explain the purpose of the code and any complex logic. Write comments at the beginning of a program, before complex sections, and when the purpose is not obvious from the code itself.
- Procedures and Functions: Break down the code into smaller, reusable modules, each with a single purpose.
Proper indentation is very important to improve readability.
- Meaningful Identifiers: Use descriptive names for variables, constants, procedures, and functions. For example,
Exam Focus
- Data Types: Examiners will expect you to know the basic data types (integer, real, char, string, Boolean) and when to use each one.
- Control Structures: Understanding and using IF, CASE, FOR, WHILE, and REPEAT-UNTIL statements is vital. Be prepared to trace code and predict output.
- String Handling: Be able to use LENGTH, SUBSTRING, UPPER, and LOWER functions.
- Operators: Know the arithmetic, logical, and Boolean operators, including operator precedence.
- Procedures/Functions: Understand their purpose, how to define and call them (with and without parameters), and the difference between local and global variables.
- Library Routines: Be familiar with MOD, DIV, ROUND, and RANDOM and their uses.
- Readability: Always use meaningful variable names, comments and indentation in your pseudocode.
Common Mistakes to Avoid
- ❌ Wrong: Giving hard disk drive as a storage example when asked about RAM or ROM. ✓ Right: RAM and ROM are primary storage examples; a hard disk drive is secondary storage.
- ❌ Wrong: Vague descriptions of how a system operates. ✓ Right: Provide detailed explanations including the specific type of sensor used and how it sends data to the microprocessor.
- ❌ Wrong: Answering 'USB' when asked about storage devices. ✓ Right: USB memory stick or USB hard drive.
- ❌ Wrong: Explaining what would be stored in RAM or ROM instead of explaining why it is primary storage. ✓ Right: Explain that RAM and ROM are directly accessible to the CPU and have fast access times.
- ❌ Wrong: Not understanding the difference between RAM and ROM. ✓ Right: RAM is volatile and used for actively running programs and data. ROM is non-volatile and stores the boot-up instructions for the computer.
Exam Tips
- When writing pseudocode, follow a consistent and clear style. Use indentation to show structure.
- Read the question carefully to determine the most appropriate data types and control structures to use.
- When tracing code, keep track of the values of all variables at each step.
- Practice writing procedures and functions to break down problems into smaller, more manageable parts.