Programming Fundamentals
Every app, game and website you use is built from a small number of simple ideas. Master these building blocks and you can read and write code in almost any language. This lesson covers four of them: storing information in variables and constants, changing that information with operators, and controlling the order in which instructions run.
Storing data: variables and constants
A variable is like a labelled box in the computer’s memory. The label is its name, and inside the box is a value your program can read or change at any time. A game might use a variable called score that starts at zero and grows as the player collects points. Because the value can change while the program is running, we say a variable is mutable.
A constant is also a named value, but it is set once and never changes while the program runs. Good examples are a rate of VAT, the value of pi, or the maximum number of lives in a game. Using a constant makes a program easier to read and safer, because an important value cannot be altered by mistake.
The key difference is change. A variable’s value can change while the program is running; a constant’s value is fixed and stays the same throughout.
Assignment, input and output
To put a value into a variable we use assignment, written with a single equals sign. The computer works out the right-hand side first, then stores the result in the variable named on the left. This is why score = score + 10 makes sense: it means ‘take the current score, add ten, and store the answer back in score’.
score = 0
name = input("Enter your name: ")
score = score + 10
print(name)
print(score)
Most programs also need to talk to the user. An input command reads something typed by the user so you can store it in a variable, while an output command (often written as print) displays a value on screen. Together, input, assignment and output let a program take data in, work on it, and show a result.
In everyday maths, x = x + 1 is impossible — a number cannot equal itself plus one. In programming the equals sign is not a question about whether two things are the same; it is an instruction to store a value. x = x + 1 tells the computer to take the current value of x, add one, and put the answer back. To test whether two values are the same, programmers use the double equals operator == instead.
Operators
An operator is a symbol or keyword that acts on data. The arithmetic operators do the maths you already know — addition with +, subtraction with -, multiplication with * and division with / — plus three that are especially useful in programming.
- Normal division uses
/and can give a decimal, so seventeen divided by five is3.4. DIVgives the whole-number quotient, meaning how many whole times one number goes into another, so17 DIV 5is3.MODgives the remainder, or modulo, that is left over, so17 MOD 5is2.^raises a number to a power, known as exponentiation, so3 ^ 2is9.
DIV and MOD work as a pair. DIV gives the whole number of times a division fits, and MOD gives what is left over. So 23 DIV 4 is 5, and 23 MOD 4 is 3.
Comparison operators ask a true-or-false question about two values. They are: equal to ==, not equal to !=, less than <, less than or equal to <=, greater than >, and greater than or equal to >=. Notice that testing for equality uses two equals signs, which keeps it clearly separate from assignment.
Boolean operators let you combine these true-or-false results. AND is true only when both conditions are true, OR is true when at least one condition is true, and NOT reverses a result, turning true into false and false into true.
Imagine a website that lets someone register only if they are aged 13 or over and have ticked the terms box. In code this becomes age >= 13 AND agreed == true. Both parts must be true for the whole condition to be true. If you swapped AND for OR, a person could register by doing just one of the two things — which is exactly why choosing the right Boolean operator matters.
The three programming constructs
Every program controls the order its instructions run using just three building blocks, called constructs. Almost all software is these three, combined and nested inside one another.
Sequence simply means the computer carries out instructions one after another, from top to bottom, in the order they are written. On its own, sequence always does exactly the same thing every time it runs.
Selection lets a program choose between different paths depending on a condition. It is built with if, elseif and else, or with a switch/case statement when you are checking one value against many possible options.
if score >= 50 then
print("Pass")
elseif score >= 40 then
print("Near miss")
else
print("Try again")
endif
Iteration, also called looping, repeats a block of code so you do not have to write it out many times. There are two kinds. A count-controlled loop repeats a set number of times using a counter, which is ideal when you know exactly how many repeats you need. A condition-controlled loop keeps going until a condition is met, so the number of repeats can vary each time the program runs.
// count-controlled loop
for i = 1 to 5
print("Hello")
next i
// condition-controlled loop
answer = ""
while answer != "yes"
answer = input("Ready? ")
endwhile
Sequence, selection and iteration are the three constructs that control the flow of any program. Sequence runs steps in order, selection chooses between paths, and iteration repeats steps. Nearly every program is a mix of these three.
These building blocks are deliberately simple, yet they combine to make everything from a calculator to a search engine. When you plan a program, ask yourself three questions: what data do I need to store, what operators will change it, and which constructs will control when each step runs?