A compiler translates the whole program from a high-level language into machine code in one go, before the program runs. An interpreter translates and runs the program one statement at a time. Exam questions ask you to compare the two, often through error reporting, speed or distribution.
This lesson follows operating system and application roles in the software and systems module.
Why do programs need translating?
The processor only understands machine code, which is binary instructions. People write in high-level languages such as Python or pseudocode-like languages because they are readable. A translator closes that gap.
The program you write is the source code. What the compiler produces is the object code, or executable. An interpreter produces no separate file.
How do the two behave?
| Feature | Compiler | Interpreter |
|---|---|---|
| Translation | Whole program at once | One statement at a time, while running |
| Output | A separate executable file | No file produced |
| Errors | Lists errors after attempting the whole program; nothing runs until they are fixed | Stops at the first error; earlier lines have already run |
| Running again | Runs the executable directly, no translator needed | Translates again every time |
| Speed of running | Faster | Slower |
| Sharing | The user gets the executable and not the source code | The user needs the interpreter and the source |
Worked example
A short program has an error on line 4, because totl was typed instead of total.
1 total ← 0
2 OUTPUT "Start"
3 total ← total + 5
4 OUTPUT totl
5 OUTPUT "Done"
Interpreter, traced:
| Step | Line | What happens | Screen output |
|---|---|---|---|
| 1 | 1 | total is set to 0 | none |
| 2 | 2 | Prints Start | Start |
| 3 | 3 | total becomes 5 | none |
| 4 | 4 | totl is not defined: error reported, run stops | error message |
| 5 | 5 | Never reached | none |
The user sees “Start” and then an error message pointing at line 4.
Compiler: the compiler reads all five lines first, finds the undefined totl on line 4, and reports it. No executable is produced, so nothing runs at all and “Start” is never displayed. After you fix line 4 and compile again, the executable runs and displays Start, then 5, then Done.
The mistake to watch for
Mistaken answer: “A compiler translates the program line by line and stops when it finds an error.”
That describes an interpreter. The compiler works on the whole program and normally reports its errors together, so you can fix several at once. Check your answer with one question: did the code run while it was being translated? If yes, it is an interpreter.
A second slip is saying “the interpreter is faster because it runs straight away”. It starts sooner, but each run is slower, because the translation is repeated.
Check yourself
1. A company wants to sell its game without letting buyers see the source code. Which translator suits, and why?
Show answer
A compiler. It produces an executable file made of machine code, so the company can distribute that file and keep the source code private.
2. A student is learning to program and wants to test one line at a time as it is written. Which translator suits, and why?
Show answer
An interpreter. It runs each statement straight away and stops at the first error, so the student sees at once which line is faulty.
3. Using the program above with the typo fixed (OUTPUT total), what is displayed?
Show answer
Line 2 prints Start. Line 3 makes total equal to 0 + 5 = 5. Line 4 prints 5. Line 5 prints Done. Output: Start, 5, Done.
Where this leads next
Next, see how the processor reacts to events while programs run, in explain an interrupt using a scenario. If you want to watch a loop trace step by step, try the restricted pseudocode trace trainer.
Comparison questions reward exact contrast words such as “whole program” and “one statement at a time”. A teacher in online one-to-one Computer Science tuition can help you tighten those phrases.