Computer Science · Topic 03

Programming Fundamentals

Learn how programs store information, make decisions, repeat tasks, organise instructions, and solve problems. The examples use pseudocode rather than a specific programming language.

1. What is programming?

Programming is the process of designing and writing instructions that a computer can execute. A program may perform a calculation, process data, display information, or control a system.

Example: Calculate an average

START
  INPUT mark1, mark2, mark3
  average = (mark1 + mark2 + mark3) / 3
  DISPLAY average
END

The program receives three marks, calculates their average, and displays the result.

2. Variables and constants

A variable is a named place used to hold a value that may change while a program runs. A constant represents a value that should remain fixed within the relevant program context.

score = 10
score = score + 5
DISPLAY score

CONSTANT TAX_RATE = 0.05

The final score is 15. TAX_RATE is intended to remain fixed. Exact declaration syntax depends on the programming language.

Rules for naming variables

3. Data types

A data type describes the kind of value a variable can represent and the operations available for it.

TypeExamplePurpose
Integer25Whole numbers
Floating-point number3.14Approximate real-number values
String"Hello"Text
BooleanTRUEA logical true/false value
Character'A'A single character in languages that provide this type

Languages differ: some use static type declarations, while others infer or determine types at runtime. Floating-point arithmetic may introduce small rounding errors.

4. Operators

Operators perform calculations, comparisons, or logical operations.

CategoryOperatorsExample
Arithmetic+, −, ×, ÷8 + 2 = 10
RemainderMOD or %10 MOD 3 = 1
Comparison==, !=, <, >, <=, >=8 > 2 is TRUE
LogicalAND, OR, NOTTRUE AND FALSE is FALSE
Assignment= in this pseudocodetotal = 20

Operator symbols vary across languages. In many languages, = assigns a value, while == compares two values.

5. Input and output

Input brings information into a program. Output communicates a result to the user or another system.

START
  INPUT name
  DISPLAY "Welcome, " + name
END

If the input is "Aman", the output is "Welcome, Aman".

Real programs should validate input when necessary. For example, a program expecting a number should handle invalid text appropriately.

6. Conditional statements

Conditions let a program choose which instructions to execute based on whether a logical expression is true or false.

Example: Check an age

INPUT age

IF age >= 18
  DISPLAY "Adult"
ELSE
  DISPLAY "Under 18"
END IF

This example illustrates a simple threshold. Real eligibility rules depend on the specific context and applicable law.

Multiple conditions

INPUT score

IF score >= 90
  DISPLAY "Grade A"
ELSE IF score >= 75
  DISPLAY "Grade B"
ELSE IF score >= 50
  DISPLAY "Grade C"
ELSE
  DISPLAY "Needs improvement"
END IF

Order matters: the first matching branch is selected. Here, the score ranges are handled from highest to lowest.

7. Loops

Loops repeat a block of instructions. They are useful for processing lists, repeating calculations, and performing tasks until a condition is met.

For loop

FOR i = 1 TO 5
  DISPLAY i
END FOR

Displays the numbers 1 through 5, assuming the inclusive range shown in this pseudocode.

While loop

count = 1

WHILE count <= 3
  DISPLAY count
  count = count + 1
END WHILE

The loop stops when count becomes 4. Updating the variable prevents an endless loop in this example.

Common loop types

  • For: Often used when iterating over a known range or collection.
  • While: Repeats while a condition remains true.
  • Do-while: In languages that support it, checks the condition after the body, so the body runs at least once.

8. Functions

A function is a reusable block of code that performs a defined task. Functions can accept inputs called parameters and may return a result.

FUNCTION square(number)
  RETURN number * number
END FUNCTION

result = square(4)
DISPLAY result

The function returns 16 when called with 4.

Functions help programmers:

9. Arrays and strings

An array or list can store multiple values in an ordered collection. A string represents text.

numbers = [4, 7, 2, 9]
total = 0

FOR EACH number IN numbers
  total = total + number
END FOR

DISPLAY total

The output is 22.

Indexing conventions vary. Many languages begin array indices at 0, while some educational pseudocode and languages use 1.

10. Errors and debugging

Programs may fail or produce incorrect results. Three common error categories are:

Syntax error

The code violates the language's grammar or rules.

Runtime error

An error occurs while the program is executing.

Logic error

The program runs but produces an incorrect result.

Debugging

Finding, understanding, and correcting defects in a program.

A useful debugging process is to reproduce the problem, inspect inputs and intermediate values, identify the cause, make a correction, and test the program again.

11. Quick knowledge check

Try each question before revealing its answer.

Question 1: What is the purpose of a variable?

It gives a name to a value that a program can store and use.

Question 2: What does a condition do?

It allows a program to select instructions based on whether a logical expression is true or false.

Question 3: How many times does this loop run: FOR i = 1 TO 4?

Four times in the inclusive-range pseudocode used here.

Question 4: Why are functions useful?

They organise logic into reusable units, making programs easier to test, read, and maintain.

Question 5: A program runs but calculates 2 + 2 as 5. What kind of error is this likely to be?

A logic error: the program runs but produces the wrong result.

Key takeaways