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:
* 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.
### 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:
### 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:
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
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.