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.

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.

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