Computer Science (4CP0)
Topic 3 of 6Pearson EdExcel

Programming Concepts

Core programming constructs: variables for data, selection for decisions, iteration for repetition.

### Introduction to Programming Concepts


Programming is the process of writing instructions for a computer to execute. These instructions are built using fundamental concepts that control the flow of the program and manage its data. For the Pearson EdExcel IGCSE, mastering variables, selection, iteration, and subprograms is essential. These are the building blocks of all software, from simple calculators to complex operating systems.


### 1. Variables and Constants


At the heart of any program is data. We use variables and constants to store this data in the computer's memory.


* A variable is a named memory location that stores a value which can change while the program is running. Think of it as a labeled box where you can put different items at different times.

* A constant is a named memory location that stores a fixed value that cannot be changed during program execution. This is useful for values that must remain consistent, such as the value of Pi (3.142) or the GST rate in Pakistan.


Data Types:

Every variable or constant must have a defined data type, which tells the computer what kind of data it can hold. This is crucial for memory allocation and preventing errors.


* Integer: Stores whole numbers, positive or negative (e.g., 10, -5, 0). Used for counting items, like the number of students in a class.

* Real (or Float): Stores numbers with a decimal point (e.g., 98.6, -15.25). Used for measurements like temperature or bank balances.

* Boolean: Stores one of only two possible values: True or False. Essential for decision-making in selection and iteration.

* String: Stores a sequence of characters, such as text (e.g., "Karachi", "Computer Science 4CP0").

* Character (Char): Stores a single letter, number, or symbol (e.g., 'A', '7', '$').


Assignment:

The process of giving a variable a value is called assignment. The EdExcel pseudocode uses the left arrow `←` for this.


`// Example of declaring and assigning variables`

`DECLARE studentName : STRING`

`DECLARE age : INTEGER`

`DECLARE hasPassed : BOOLEAN`


`studentName ← "Ahmed"`

`age ← 16`

`hasPassed ← True`


### 2. Selection


Selection statements allow a program to make decisions and execute different blocks of code based on whether a condition is True or False. This is also known as conditional logic.


IF...THEN...ELSE

This is the most common selection structure. The `ELSE` part is optional.


* `IF...THEN...ENDIF`: Executes code only if the condition is true.

* `IF...THEN...ELSE...ENDIF`: Executes one block of code if the condition is true, and a different block if it is false.


`// Example: Checking pass/fail status`

`DECLARE marks : INTEGER`

`marks ← 65`

`IF marks >= 50 THEN`

` OUTPUT "Congratulations, you have passed."`

`ELSE`

` OUTPUT "You need to retake the exam."`

`ENDIF`


Nested IFs and ELSEIF

For multiple conditions, you can use `ELSEIF` to create a clean structure without deep nesting.


`// Example: Grading based on marks`

`IF marks >= 85 THEN`

` OUTPUT "Grade: A*"`

`ELSEIF marks >= 75 THEN`

` OUTPUT "Grade: A"`

`ELSEIF marks >= 60 THEN`

` OUTPUT "Grade: B"`

`ELSE`

` OUTPUT "Grade: C or below"`

`ENDIF`


CASE...OF...OTHERWISE

This structure is a tidy alternative to a long `ELSEIF` chain when checking a single variable against multiple possible values.


`// Example: Menu selection`

`DECLARE choice : INTEGER`

`OUTPUT "Enter 1 for Urdu, 2 for English"`

`INPUT choice`

`CASE choice OF`

` 1 : OUTPUT "آپ نے اردو کا انتخاب کیا ہے۔"`

` 2 : OUTPUT "You have selected English."`

`OTHERWISE`

` OUTPUT "Invalid selection."`

`ENDCASE`


Common Exam Trap: Confusing the assignment operator (`←`) with the comparison operator for equality (`=`). In an `IF` condition, always use `=`. For example, `IF userChoice = 1 THEN...`.


### 3. Iteration


Iteration, or looping, allows a block of code to be executed repeatedly. This is essential for processing lists of data, repeating user input prompts, or running simulations.


Count-Controlled Loop: FOR...TO...NEXT

This loop runs a fixed number of times. It uses a counter variable that starts at one value, increments, and stops at an end value.


`// Example: Printing times tables`

`DECLARE i : INTEGER`

`FOR i ← 1 TO 10`

` OUTPUT 5, " x ", i, " = ", 5*i`

`NEXT i`


Condition-Controlled Loops

These loops run as long as a certain condition is met. The number of repetitions is not known beforehand.


  • WHILE...DO...ENDWHILE (Pre-condition loop): The condition is checked *before* each iteration. If the condition is initially false, the loop will never execute.

  • `// Example: Password validation`

    `DECLARE password : STRING`

    `password ← ""`

    `WHILE password <> "Edexcel123"`

    ` OUTPUT "Enter password:"`

    ` INPUT password`

    `ENDWHILE`

    `OUTPUT "Access granted."`


  • REPEAT...UNTIL (Post-condition loop): The condition is checked *after* each iteration. This guarantees the loop body will execute at least once.

  • `// Example: Menu choice validation`

    `DECLARE choice : STRING`

    `REPEAT`

    ` OUTPUT "Do you want to play again? (Y/N)"`

    ` INPUT choice`

    `UNTIL choice = "Y" OR choice = "N"`


    Common Misconception: The key difference between `WHILE` and `REPEAT...UNTIL` is the point of condition testing. Use `WHILE` when the loop might not need to run at all. Use `REPEAT...UNTIL` when you need the code to run at least one time, such as for initial data input.


    ### 4. Subprograms


    As programs grow, it's vital to break them into smaller, manageable, and reusable chunks called subprograms. This practice is known as modular programming.


    * Procedure: A subprogram that performs a specific task but does not return a value to the part of the code that called it. It is called using the `CALL` keyword.


    `// Procedure to display a welcome message`

    `PROCEDURE displayWelcome(name : STRING)`

    ` OUTPUT "Welcome, ", name`

    `ENDPROCEDURE`


    `// Calling the procedure`

    `CALL displayWelcome("Fatima")`


    * Function: A subprogram that performs a task and returns a single value. It is used in expressions, often on the right side of an assignment statement.


    `// Function to calculate the area of a circle`

    `FUNCTION circleArea(radius : REAL) RETURNS REAL`

    ` DECLARE area : REAL`

    ` area ← 3.142 * radius * radius`

    ` RETURN area`

    `ENDFUNCTION`


    `// Calling the function`

    `DECLARE myArea : REAL`

    `myArea ← circleArea(10.0)`

    `OUTPUT "The area is: ", myArea`


    Subprograms make code easier to read, test, and debug. They also promote code reuse, saving time and reducing errors.```

    IF score >= 50 THEN

    OUTPUT "Pass"

    ELSE

    OUTPUT "Fail"

    ENDIF

    ```


    Iteration (loops):

  • FOR loop: Fixed number of repetitions
  • WHILE loop: Repeat while a condition is True (check BEFORE loop)
  • REPEAT...UNTIL: Repeat until condition (check AFTER loop — always runs at least once)

  • Subprograms:

  • Procedures: Block of code with a name, no return value
  • Functions: Returns a value (RETURN statement)

  • Local vs global variables: Local variables only exist inside the subprogram. Global variables can be accessed anywhere.


    Validation: Checks input is reasonable (range check, type check, length check). **Verification:** Checks data entered correctly (double entry, visual check).

    Key Points to Remember

    • 1Variables: name, data type, value. Constants: never change
    • 2IF-THEN-ELSE for selection; FOR/WHILE/REPEAT for loops
    • 3Functions return values; procedures do not
    • 4Validation checks reasonableness; verification checks accuracy

    Pakistan Example

    NADRA's Verification System — Programming Logic at Scale

    When you log into the NADRA e-Sahulat portal, a function validates your CNIC: length check (13 digits), type check (integers only), range check (valid date of birth in CNIC). A Boolean function returns TRUE if valid. A procedure then logs the access attempt without returning a value. These are EdExcel programming concepts running in production systems used by millions of Pakistanis.

    Quick Revision Infographic

    Computer Science — Quick Revision

    Programming Concepts

    Key Concepts

    1Variables: name, data type, value. Constants: never change
    2IF-THEN-ELSE for selection; FOR/WHILE/REPEAT for loops
    3Functions return values; procedures do not
    4Validation checks reasonableness; verification checks accuracy

    Formulas to Know

    FOR/WHILE/REPEAT for loops
    Pakistan Example

    NADRA's Verification System — Programming Logic at Scale

    When you log into the NADRA e-Sahulat portal, a function validates your CNIC: length check (13 digits), type check (integers only), range check (valid date of birth in CNIC). A Boolean function returns TRUE if valid. A procedure then logs the access attempt without returning a value. These are EdExcel programming concepts running in production systems used by millions of Pakistanis.

    SeekhoAsaan.com — Free RevisionProgramming Concepts Infographic

    Test Your Knowledge!

    10 Beginner10 Intermediate10 Advanced
    Start 30-Question Quiz