1.2.4 Types of Programming Language
1.2.4(a) Programming Paradigms
Different problems require different approaches to solve them efficiently. Programming paradigms provide frameworks for thinking about and structuring solutions.
Imperative vs Declarative
Imperative Paradigms - Focus on HOW to solve a problem
• You specify the exact steps the computer must take
• Like giving detailed directions: "Turn left, walk 50m, turn right..."
• Examples: Procedural, Object-Oriented
Declarative Paradigms - Focus on WHAT the outcome should be
• You describe what you want, not how to get it
• Like saying: "Get me to the train station"
• Examples: Functional, Logic programming, SQL
1.2.4(b) Procedural Languages
Procedural programming is one of the most common paradigms, especially for learning to program.
• Programs are sequences of instructions
• Uses the three constructs: sequence, selection, iteration
• Code organised into procedures/functions
• Data and procedures are separate
• Top-down design approach
• Examples: C, Pascal, Python (can be procedural)
// Data is separate from procedures
studentScores = [85, 72, 91, 68, 77]
// Procedure to calculate average
function calculateAverage(scores):
total = 0
for each score in scores do
total = total + score
end for
return total / length(scores)
end function
// Procedure to determine grade
function getGrade(average):
if average >= 70 then
return "A"
elseif average >= 60 then
return "B"
elseif average >= 50 then
return "C"
else
return "Fail"
end if
end function
// Main program flow
avg = calculateAverage(studentScores)
grade = getGrade(avg)
print("Average: " + avg + ", Grade: " + grade)
• Small to medium programs
• Problems with clear step-by-step solutions
• Learning to program
• Scripts and automation tasks
1.2.4(c) Assembly Language
Assembly language provides the closest human-readable representation of what the CPU actually executes.
Relationship to Machine Code
• Machine code: Binary instructions CPU directly executes (e.g., 00010101)
• Assembly: Human-readable mnemonics (e.g., ADD, LDA, STA)
• One assembly instruction = typically one machine code instruction
• Assembler converts assembly → machine code
Machine Code: 0101 0011 0100
Assembly: LDA 52
Meaning: Load value from address 52 into accumulator
Why Use Assembly?
| Advantages | Disadvantages |
|---|---|
| Maximum efficiency - no compiler overhead | Difficult to write and debug |
| Direct hardware control | Not portable (CPU-specific) |
| Precise timing control | Time-consuming development |
| Small code size | Easy to make errors |
| Used in device drivers, embedded systems, OS kernels | Hard to maintain |
1.2.4(d) Modes of Addressing Memory
When an instruction needs to access data, it must specify WHERE that data is. Different addressing modes provide different ways to specify the location.
Immediate Addressing
ADD #5 // Add the literal value 5 to accumulator
LDA #100 // Load the literal value 100 into accumulator
The # symbol indicates immediate addressing. The value 5 or 100 is used directly.
Direct Addressing
ADD 50 // Add the value STORED AT address 50 to accumulator
LDA 100 // Load the value STORED AT address 100 into accumulator
If address 50 contains the value 7, then ADD 50 adds 7.
Indirect Addressing
| Memory address | Value stored |
|---|---|
| 50 | 75 |
| 75 | 42 |
LDA @50 // indirect addressing, @ symbol
1. Look at address 50 → find value 75
2. Use 75 as the actual address
3. Look at address 75 → find value 42
4. Load 42 into accumulator
Result: Accumulator = 42
Indexed Addressing
| Item | Value |
|---|---|
| Base address | 100 |
| Index register (X) | 3 |
| Effective address | 100 + 3 = 103 |
LDA 100,X
Action: Load value from address 103 into accumulator.
Why is this useful? Perfect for arrays - the base address is where the array starts, and the index moves through each element.| X value | Instruction | Address used | Value loaded |
|---|---|---|---|
| 0 | LDA 100,X | 100 | 10 |
| 1 | LDA 100,X | 101 | 20 |
| 2 | LDA 100,X | 102 | 30 |
Addressing Modes Summary
• Operand Contains: The actual value
• Example: ADD #5
• Use Case: Constants, fixed values
Direct
• Operand Contains: Address of data
• Example: ADD 50
• Use Case: Simple variables
Indirect
• Operand Contains: Address of address
• Example: ADD @50
• Use Case: Pointers, dynamic access
Indexed
• Operand Contains: Base + index
• Example: ADD 100,X
• Use Case: Arrays, tables
• Immediate = the value is RIGHT THERE in the instruction
• Direct = go DIRECTLY to that address for the value
• Indirect = indirect route - address gives you another address
• Indexed = base + index (like array access)
1.2.4(e) Object-Oriented Languages (OOP)
Object-Oriented Programming is a paradigm that organises code around objects - entities that combine data and behaviour.
Classes and Objects
An object is a specific instance of a class - a real entity created from the blueprint.
// CLASS - the blueprint
class Dog:
// Attributes (data)
private name
private breed
private age
// Constructor - creates new objects
public procedure new(givenName, givenBreed, givenAge):
name = givenName
breed = givenBreed
age = givenAge
end procedure
// Methods (behaviour)
public procedure bark():
print(name + " says: Woof!")
end procedure
public function getAge():
return age
end function
end class
// OBJECTS - instances of the class
myDog = new Dog("Rex", "German Shepherd", 3)
yourDog = new Dog("Bella", "Labrador", 5)
// Using the objects
myDog.bark() // Output: Rex says: Woof!
print(yourDog.getAge()) // Output: 5
Encapsulation
• Private attributes/methods: Only accessible within the class
• Public attributes/methods: Accessible from outside the class
Why Encapsulate?
• Protects data from invalid modifications
• Hides implementation details
• Makes code easier to maintain
• Provides controlled access through methods (getters/setters)
class BankAccount:
private balance // Cannot be accessed directly from outside
public procedure deposit(amount):
if amount > 0 then
balance = balance + amount
end if
end procedure
public function getBalance():
return balance
end function
end class
// Usage
account = new BankAccount()
// account.balance = 1000000 // ERROR - balance is private!
account.deposit(500) // OK - uses public method
print(account.getBalance()) // OK - uses public method
Inheritance
// SUPERCLASS (parent)
class Animal:
protected name
protected age
public procedure makeSound():
print("Some sound")
end procedure
end class
// SUBCLASS (child) - inherits from Animal
class Dog inherits Animal:
private breed
// Override parent method
public procedure makeSound():
print(name + " barks: Woof!")
end procedure
// Additional method specific to Dog
public procedure fetch():
print(name + " fetches the ball")
end procedure
end class
// SUBCLASS (child)
class Cat inherits Animal:
public procedure makeSound():
print(name + " meows: Meow!")
end procedure
end class
// Usage
myDog = new Dog()
myDog.name = "Rex" // Inherited from Animal
myDog.makeSound() // Output: Rex barks: Woof!
myDog.fetch() // Dog-specific method
myCat = new Cat()
myCat.name = "Whiskers" // Inherited from Animal
myCat.makeSound() // Output: Whiskers meows: Meow!
Polymorphism
// Both Dog and Cat inherit from Animal
animals = [new Dog("Rex"), new Cat("Whiskers"), new Dog("Bella")]
// Polymorphism - same method call, different behaviour
for each animal in animals do
animal.makeSound()
end for
// Output:
// Rex barks: Woof!
// Whiskers meows: Meow!
// Bella barks: Woof!
// The code doesn't need to know the specific type -
// each object responds appropriately
OOP Principles Summary
• Description: Bundle data and methods, hide internals
• Benefit: Data protection, maintainability
Inheritance
• Description: Child classes inherit from parent
• Benefit: Code reuse, hierarchy
Polymorphism
• Description: Same interface, different implementations
• Benefit: Flexibility, extensibility
Abstraction
• Description: Hide complex details, show only essentials
• Benefit: Simplicity, focus
• OOP: Data and methods together in objects
• Procedural: Data and procedures separate
• OOP better for: Large projects, code reuse, modelling real-world entities
• Procedural better for: Small scripts, simple programs, learning