Computer Science (9618)
Topic 9 of 17Cambridge A Levels

Programming Paradigms and Fundamentals

Explore core programming styles, constructs, file/error handling, and recursion.

This topic introduces the foundational principles and styles of computer programming, essential for developing robust and efficient software solutions as required by the Cambridge A Levels Computer Science syllabus.


### Programming Paradigms

A programming paradigm is a style or 'way' of programming. There are several paradigms, but we focus on three major types:


  • Procedural Programming: This is an imperative paradigm where programs are built from one or more procedures (also known as subroutines or functions). A program is a series of sequential, top-down instructions. Data is often stored in global variables and passed to procedures that perform operations on it. Key features include procedure calls, loops, and conditional statements. Languages like C, Pascal, and early versions of BASIC are classic examples.

  • Object-Oriented Programming (OOP): This paradigm models the real world by organising software design around data, or objects, rather than functions and logic. An object is an instance of a class, which acts as a blueprint. OOP is built upon four core principles:
  • * Encapsulation: Bundling data (attributes) and methods (functions) that operate on the data into a single unit (a class). This restricts direct access to some of an object's components, which is a key part of data hiding.

    * Abstraction: Hiding complex implementation details and showing only the essential features of the object. For example, you use a TV remote without needing to know how its internal circuitry works.

    * Inheritance: A mechanism where a new class (subclass or child class) derives attributes and methods from an existing class (superclass or parent class). This promotes code reusability.

    * Polymorphism: The ability of an object to take on many forms. A single function or method can work with different data types or classes. For example, a `draw()` method could draw a circle, square, or triangle depending on the object type.


  • Declarative Programming: This paradigm focuses on *what* the program must accomplish without prescribing *how* to do it (the control flow). The logic of the computation is expressed without describing its step-by-step execution. Sub-paradigms include logic programming (e.g., Prolog), where programs are a set of logical assertions, and functional programming (e.g., Haskell), which treats computation as the evaluation of mathematical functions.

  • ### Fundamental Programming Constructs

    Regardless of the paradigm, all programs are built using fundamental constructs:

    * Sequence: Instructions are executed one after another in the order they are written.

    * Selection: Used for decision-making. The primary constructs are IF...THEN...ELSE for binary choices and CASE...SWITCH for multiple choices based on a variable's value.

    * Iteration (Loops): Allows a block of code to be executed repeatedly. The main types are:

    * FOR loop: A count-controlled loop that repeats a fixed number of times.

    * WHILE loop: A condition-controlled loop that repeats as long as a condition is true (tested at the start).

    * REPEAT...UNTIL loop: A condition-controlled loop that repeats until a condition becomes true (tested at the end), guaranteeing at least one execution.


    ### File Handling

    File handling is crucial for persistent storage, allowing data to be saved after the program terminates. The standard process involves:

  • Opening a file: You must specify the filename and the mode (read, write, or append). Writing overwrites existing content, while appending adds new content to the end.
  • Processing the file: This involves either reading data from the file into variables or writing data from variables into the file.
  • Closing the file: This is a critical step to ensure data is saved correctly and system resources are freed.

  • ### Exception Handling

    An exception is a runtime error that disrupts the normal flow of a program. Exception handling is a mechanism to gracefully manage these errors without crashing the application. The most common structure is the try-catch block (or `try-except` in Python):

    * The try block contains the code that might potentially raise an exception.

    * If an exception occurs, the program jumps to the corresponding catch (or except) block, which contains the code to handle the error (e.g., display an error message).

    * An optional finally block can be used for code that must execute regardless of whether an exception occurred, such as closing a file.


    ### Recursion

    A recursive function is a function that calls itself. To avoid an infinite loop, recursion requires two components:

  • Base Case: A condition that stops the recursion. When the base case is met, the function returns a value without making another recursive call.
  • Recursive Step: The part of the function that calls itself, but with modified arguments that move it closer to the base case.
  • A classic example is calculating a factorial: `Factorial(n) = n * Factorial(n-1)`, with the base case being `Factorial(0) = 1`.

    Key Points to Remember

    • 1Paradigms define programming style: Procedural (step-by-step), OOP (object-based), and Declarative (what, not how).
    • 2Object-Oriented Programming (OOP) is built on four pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction.
    • 3All programs use fundamental constructs: sequence, selection (IF/CASE), and iteration (FOR/WHILE/REPEAT).
    • 4File handling enables persistent data storage through key operations: open, read, write, and close.
    • 5Exception handling manages runtime errors using 'try-catch' blocks to prevent program crashes.
    • 6Recursion is a problem-solving technique where a function calls itself, requiring a base case to terminate.
    • 7Data is stored in variables and constants, and classified by data types (e.g., Integer, Real, String, Boolean).

    Pakistan Example

    NADRA Citizen Database System

    The National Database and Registration Authority (NADRA) system can be viewed through a programming paradigm lens. An **Object-Oriented** approach is ideal: each Pakistani citizen can be represented as a `Citizen` **object**, created from a `Citizen` **class**. This class would **encapsulate** attributes like CNIC, name, and biometrics. **Inheritance** could be used to create specialized classes like `OverseasPakistani` from the base `Citizen` class. When a new citizen is registered, the system performs **file handling** by writing a new record to its database. **Exception handling** is critical; if a user enters an incorrectly formatted CNIC during a search, a 'try-catch' block would handle this error gracefully, prompting for valid input instead of crashing the verification terminal.

    Quick Revision Infographic

    Computer Science — Quick Revision

    Programming Paradigms and Fundamentals

    Key Concepts

    1Paradigms define programming style: Procedural (step-by-step), OOP (object-based), and Declarative (what, not how).
    2Object-Oriented Programming (OOP) is built on four pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction.
    3All programs use fundamental constructs: sequence, selection (IF/CASE), and iteration (FOR/WHILE/REPEAT).
    4File handling enables persistent data storage through key operations: open, read, write, and close.
    5Exception handling manages runtime errors using 'try-catch' blocks to prevent program crashes.
    6Recursion is a problem-solving technique where a function calls itself, requiring a base case to terminate.
    Pakistan Example

    NADRA Citizen Database System

    The National Database and Registration Authority (NADRA) system can be viewed through a programming paradigm lens. An **Object-Oriented** approach is ideal: each Pakistani citizen can be represented as a `Citizen` **object**, created from a `Citizen` **class**. This class would **encapsulate** attributes like CNIC, name, and biometrics. **Inheritance** could be used to create specialized classes like `OverseasPakistani` from the base `Citizen` class. When a new citizen is registered, the system performs **file handling** by writing a new record to its database. **Exception handling** is critical; if a user enters an incorrectly formatted CNIC during a search, a 'try-catch' block would handle this error gracefully, prompting for valid input instead of crashing the verification terminal.

    SeekhoAsaan.com — Free RevisionProgramming Paradigms and Fundamentals Infographic

    Test Your Knowledge!

    5 questions to test your understanding.

    Start Quiz