How Computers Run Code
Before writing more code, it helps to know what happens inside the computer when you press "Run".
The Main Parts of a Computer
You do not need to be an engineer, but a few parts matter to a programmer:
| Part | What it does | Everyday comparison |
|---|---|---|
| CPU (processor) | Follows instructions, very fast | The cook in a kitchen |
| RAM (memory) | Holds data while a program runs | The kitchen counter |
| Storage (SSD/hard disk) | Keeps files even when power is off | The cupboard |
| Input / output | Keyboard, screen, speakers | Taking orders and serving food |
When you run a program, it is loaded from storage into memory, and the CPU starts following its instructions one by one. The data your program uses, like a name or a price, sits in memory while the program runs.
From Your Code to Machine Code
The CPU only understands machine code: long patterns of 1s and 0s. Nobody wants to write that by hand. So we write in a language like Python, and a special program translates it for us. There are two main ways to do this.
Compilers
A compiler translates the whole program into machine code first, and saves it as a file you can run. C, C++ and Go work this way. It is like translating a whole book before anyone reads it.
Interpreters
An interpreter reads your code and runs it step by step, translating as it goes. Python works this way. JavaScript in the browser is run by an engine that does a mix of both, but for a beginner it feels like an interpreter.
You do not need to choose between compilers and interpreters. The language you pick decides it for you. Just know that "running code" always involves some translation.
Code Runs from Top to Bottom
The computer follows your lines in order. Watch how changing the order changes the result:
Example
print("1. Open the shop")
print("2. Serve customers")
print("3. Close the shop")1. Open the shop 2. Serve customers 3. Close the shop
The same program in JavaScript:
Example
console.log("1. Open the shop");
console.log("2. Serve customers");
console.log("3. Close the shop");1. Open the shop 2. Serve customers 3. Close the shop
If you swap the lines, the computer will close the shop before opening it. It does not know what makes sense; it only follows your order.
The Computer Can Do Maths
Computers are very good at calculations. You can write maths directly in your code:
Example
print(250 + 120)
print(1000 - 350)
print(45 * 3)
print(100 / 4)370 650 135 25.0
Example
console.log(250 + 120);
console.log(1000 - 350);
console.log(45 * 3);
console.log(100 / 4);370 650 135 25
The symbols are the same in both languages: + add, - subtract, * multiply and / divide. Notice one small difference: Python shows 25.0 for division, while JavaScript shows 25.
Syntax: The Grammar of Code
Every language has strict rules about how code must be written. These rules are called syntax. A small mistake, like a missing quote, stops the program.
Example
print("Hello)SyntaxError: unterminated string literal (detected at line 1)
The closing quote is missing, so Python does not know where the text ends. This is a syntax error. The fix is simple:
Example
print("Hello")Hello
Error messages are not your enemy. They tell you what went wrong and usually which line to look at. Every programmer sees many errors every day.
A Quick Summary
- The CPU runs instructions, and memory holds data while a program runs
- Your code is translated into machine code by a compiler or an interpreter
- Lines run in order, from top to bottom
- Syntax is the grammar of a language; breaking it gives a syntax error
Exercise
Exercise
What does an interpreter do?
