What is the difference between a compiler and an interpreter?
A compiler translates all of a high-level program into machine code before it runs. An interpreter translates and runs the program one line at a time. Both are needed because a processor only understands machine code. This is National 5 Computing Science (SQA) content. Example: A program has an error on line 20. A compiler reports errors before running. An interpreter runs lines 1 to 19, then stops at line 20. Answer: compiler translates the whole program first; interpreter translates line by line.
Quick answer: A compiler and an interpreter both translate high-level code to machine code. In compiler vs interpreter terms, a compiler translates the whole program before it runs, while an interpreter translates and runs it one line at a time. This is SQA National 5 Computing Science.
What the specification expects
This topic is in the Computer systems area, Computer structure, of the SQA National 5 Computing Science course specification (C816 75). The specification lists:
- need for interpreters and compilers to translate high-level code to machine code
Method
- Say why translation is needed: processors run machine code, but programmers write high-level code.
- State what a compiler does: translates the whole program before it is run.
- State what an interpreter does: translates and runs the program line by line.
- Compare them using a clear point, such as when errors are reported.
Worked example 1: compiler vs interpreter in an exam answer
Describe the difference between a compiler and an interpreter.
- A compiler translates the whole program into machine code.
- This happens before the program runs.
- An interpreter translates one line, runs it, then moves to the next line.
Answer: A compiler translates the whole program first; an interpreter translates and runs it line by line.
Worked example 2: why translation is needed
Why must high-level code be translated?
- Programmers write in a high-level language.
- The processor can only run machine code.
Answer: Translation by a compiler or interpreter turns high-level code into machine code.
Worked example 3: an error on line 20
A program has an error on line 20 of 50. Explain what happens with an interpreter and with a compiler.
- The interpreter runs lines 1 to 19, then stops at line 20.
- The compiler translates the whole program, so it finds the error before the program runs.
Answer: The interpreter stops partway through; the compiler reports the error before running.
Common mistakes
- Saying an interpreter 'does not translate'. It translates each line.
- Mixing up which one works line by line.
- Writing only what each does and never making a comparison.
- Forgetting that both produce machine code the processor can run.
FAQs
Which is better, a compiler or an interpreter?
Neither is always better. A compiler reports errors before the program runs; an interpreter lets you run code line by line.
Does the processor run high-level code?
No. It only runs machine code.
Want one-to-one help? Find an online Computer Science tutor with ExpertTutor.
Want one-to-one support?
One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.
Find a TutorLearn face to face in Glasgow
Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.
See the centre