Programming basics

Computer Science AS · Programming. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science AS topics

The idea

A program keeps its data in named variables and constants. A variable is declared with a data type, and its value can change by assignment (←). A constant is given its value once and cannot be changed while the program runs. This prevents changes by mistake, and the value only has to be edited in one place.

Expressions use arithmetic operators (+ − * / DIV MOD), comparison operators and the logical operators AND, OR and NOT. Multiplication and division are done before addition and subtraction unless brackets say otherwise.

Built-in functions do common jobs, such as cutting up strings. In Cambridge pseudocode the first character of a string is at position 1.

In simpler words

A program stores data in named boxes. A variable's value can change; a constant's value is fixed. Each one is declared with a data type. You can do sums, compare values and join conditions with AND, OR and NOT. Built-in functions handle common jobs. In Cambridge pseudocode, the first letter in a string is position 1, not 0.

An everyday comparison

A variable is like a whiteboard: you can rub out the value and write a new one. A constant is like a printed sign: set once and not changed while the shop is open, so nobody alters it by accident, and you edit just one sign to update it. The comparison stops working because each variable also has a fixed data type that limits what it can hold.

Rules to remember

Common mistake

Reading MID(s, x, y) as "from position x to position y", or counting positions from 0. The third value is how many characters to take, and counting starts at 1.

In the exam

Use the exact pseudocode keywords: DECLARE name : TYPE, CONSTANT name = value, and the arrow ← for assignment. When asked why a constant is used, give two reasons: the value cannot be changed by mistake, and it is edited in only one place. For string functions, give the parameters in the right order, for example MID(s, x, y) where y is the number of characters. For DIV and MOD, show the working and the result, such as DIV(23, 4) = 5.

Worked example

Code ← "Lahore2026". What does MID(Code, 4, 3) & RIGHT(Code, 2) return?

  1. Number the characters from 1: L1 a2 h3 o4 r5 e6 2 0 2 6 (positions 7 to 10).
  2. MID(Code, 4, 3) takes 3 characters starting at position 4: "ore".
  3. RIGHT(Code, 2) takes the last 2 characters: "26".
  4. & joins the two strings in that order.

Answer: "ore26"

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← Introduction to Abstract Data Types (ADT)Constructs →Take a Computer Science AS test