Technology & Computing · Software & algorithms

How programming works

Most programs are built from a handful of ideas. See how variables, decisions, loops and functions fit together, and learn to read a short program line by line. No experience needed.

4 short lessons · about 16 minutes · 8 sources · updated November 2026

What you will learn

  • Explain what a variable is and what an assignment like x = x + 1 does
  • Explain why a program's lines run one at a time in order, and why the order changes the result
  • Explain how an if statement chooses which lines run by testing a condition
  • Explain how a loop repeats lines, and why something inside it must change for it to stop

Lesson 1 of 4 · 4 min

How a program remembers and changes values

Before you readIn most programming languages, what does the line x = x + 1 do?

Correct.Not quite. The answer is Adds 1 to the value in x.

It adds 1 to whatever x holds. The lesson shows why the equals sign in code gives an order instead of stating a fact.

In math class, x = x + 1 is nonsense, because no number equals itself plus one. In a program it's one of the most common lines there is.

A variable is a name attached to a stored value. The line "price = 4" is an assignment. It tells the computer to store 4 under the name price. Picture a labeled box holding a value. In Python the name works more like a tag tied to the value, but the box picture is fine for reading code.

So "x = x + 1" runs in two steps. First the computer works out the right side, using the value x holds now. Then it stores the result back in x. If x was 5, it's now 6. Programmers call this an update.

Check yourselfA program runs count = 3 and then count = count + 2. What value does count hold afterward?

Correct.Not quite. The answer is 5.

The second line reads the current value, 3, adds 2 and stores 5 back in count. The old 3 is replaced.

A program runs one statement, or instruction, at a time, from top to bottom. Each line sees the values left by the lines before it, so order matters.

Say a holds 1, b holds 2, and you want to swap them. You write "a = b" and then "b = a". After the first line, a holds 2. The second line copies a's new value into b, so both hold 2. The 1 is gone.

The fix uses a third variable to keep one value safe. Write "temp = a", then "a = b", then "b = temp". Now a holds 2 and b holds 1.

A later line never reaches back. If "y = x + 1" runs when x is 2, y holds 3, and it stays 3 even if x changes afterward.

Try to recallA program has a = 1 and b = 2, then runs a = b followed by b = a. Why doesn't that swap the two values?

The lines run in order. After "a = b", a holds 2 and the original 1 is gone. Then "b = a" copies that 2 into b, so both hold 2. A third variable has to keep one value while you move the other.

A variable is a name for a stored value, and = stores the right side under the left name. The lines run in order, so you can now predict a short program by following its values one line at a time.

Lessons in this topic

  1. How a program remembers and changes values

    See what a variable is, why "x = x + 1" makes sense in code, and why the order of lines decides what a program does.

    Essentials · 4 min

  2. How a few lines handle thousands of cases

    Learn how if statements choose which lines run and how loops repeat them, and why those two ideas, with lines in order, are enough to build any program.

    Essentials · 4 min

  3. Why programs are built from small named functions

    See how a function packages lines under a name, takes inputs and hands back a result, and why programmers build everything from small ones.

    Deeper · 4 min

  4. Reading a program you didn't write

    Trace a short program on paper to predict exactly what it prints, and learn to spot two traps that catch beginners and professionals alike.

    Deeper · 4 min

Three quick stories from this topic

The equals sign debate

Niklaus Wirth called using = to store values in code a notorious bad idea

In math, = says two sides are equal, either way round. Fortran popularized it in 1957 to mean "make the left side equal the right," so x = y and y = x do different things. Wirth said language designers copied it blindly.

Older than Fortran

The habit goes back to Heinz Rutishauser's language Superplan, designed between 1949 and 1951.

Two signs for two jobs

C tests equality with ==. ALGOL and Pascal kept = for equality and assign with := instead.

The first program's bug

The program often called the first ever had a bug, two values swapped in a division

Note G, published in 1843 with Ada Lovelace's notes on Babbage's Analytical Engine, laid out steps for computing Bernoulli numbers. The machine was never built, so nobody ran it. Modern tracing found one division done the wrong way round.

A wrong answer

With the swap, the steps would have produced -25621/630 instead of the eighth Bernoulli number, -1/30.

She caught one too

Babbage recalled in 1864 that Lovelace sent back his own working on the Bernoulli numbers after finding a grave mistake in it.

Three structures for everything

Two computer scientists proved in 1966 that three structures can build any program

Corrado Böhm and Giuseppe Jacopini showed that steps in order, choices between two paths and repetition can compute anything a computer can. Every program can be written with just those three.

Possible is not pretty

Their method of rewriting a tangled program often made it harder to read, so the proof started a debate rather than settling it.

The goto letter

Edsger Dijkstra's famous letter "Go To Statement Considered Harmful" followed in 1968.

Sources

  • Searching a million items

    See why a sorted list of a million items takes about 20 checks to search, when sorting first is worth the effort, and how a hash table skips the search altogether.

    4 lessons · 17 min

  • Why a computer can't write down one tenth

    Trace 0.1 into binary and back, and see why adding it three times doesn't give 0.3, why rounding first doesn't help, and what to do instead.

    4 lessons · 20 min

  • What an algorithm is

    An algorithm is a list of steps exact enough for a machine to follow. See how to write one, how to show it always finishes with the right answer, and why the word now covers looser things.

    4 lessons · 18 min

  • How processors execute instructions

    Follow a tiny calculation through the instructions, memory addresses and circuits that make a computer work.

    4 lessons · 22 min

  • How encryption works

    Encryption scrambles a message so only someone with the right key can read it. See how a shared key works, how strangers agree on one in public, and what end-to-end encryption in a chat app promises.

    4 lessons · 16 min

  • What a database is

    See how tables, rows and keys organize records, how one SQL question pulls out an answer, why a spreadsheet stops being enough and when other kinds of database fit better.

    4 lessons · 16 min

All technology & computing topics · Take the quiz