0
0
Intro to Computingfundamentals~6 mins

How programs are compiled or interpreted in Intro to Computing - Step-by-Step Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to tell a computer what to do, but it only understands a special language made of numbers. The problem is how to change the instructions you write into this language so the computer can follow them. Programs are either compiled or interpreted to solve this problem.
Explanation
Compilation
Compilation is like translating a whole book from one language to another before reading it. The entire program is converted into machine language all at once, creating a file the computer can run directly. This process happens before the program runs, so the computer executes the translated file quickly.
Compilation translates the entire program into machine code before running it.
Interpretation
Interpretation is like having a translator who listens to each sentence and immediately tells the listener what it means. The interpreter reads the program line by line and translates it on the fly while the program runs. This means the program starts quickly but runs slower because translation happens during execution.
Interpretation translates and runs the program line by line at runtime.
Differences in Speed and Use
Compiled programs usually run faster because the computer reads machine code directly. Interpreted programs are slower but easier to test and change because you can run them without waiting for a full translation. Some languages use a mix of both methods to balance speed and flexibility.
Compiled programs run faster; interpreted programs offer more flexibility.
Examples of Compiled and Interpreted Languages
Languages like C and C++ are usually compiled, creating fast programs. Languages like Python and JavaScript are often interpreted, making them easier to write and test quickly. Some languages, like Java, compile to an intermediate form and then interpret or compile that form on the computer.
Different languages use compilation or interpretation based on their design goals.
Real World Analogy

Imagine you want to share a recipe with a friend who only understands their own language. You can either translate the whole recipe first and give it to them (compilation), or you can explain each step as they cook (interpretation).

Compilation → Translating the entire recipe before giving it to your friend
Interpretation → Explaining each cooking step to your friend as they go
Differences in Speed and Use → Having the full recipe ready means faster cooking, but explaining step-by-step allows changes during cooking
Examples of Compiled and Interpreted Languages → Different friends prefer either a full recipe or step-by-step instructions depending on how they cook
Diagram
Diagram
┌─────────────┐        ┌───────────────┐        ┌───────────────┐
│ Source Code │───────▶│ Compiler      │───────▶│ Machine Code  │
└─────────────┘        └───────────────┘        └───────────────┘

┌─────────────┐        ┌───────────────┐        ┌───────────────┐
│ Source Code │───────▶│ Interpreter   │───────▶│ Runs Program  │
└─────────────┘        └───────────────┘        └───────────────┘
This diagram shows the two paths: compilation translates all source code first, while interpretation translates and runs code line by line.
Key Facts
CompilerA tool that translates the entire program into machine code before execution.
InterpreterA tool that translates and runs the program line by line during execution.
Machine CodeThe low-level language made of numbers that the computer's processor understands directly.
Compiled LanguageA programming language typically translated fully into machine code before running.
Interpreted LanguageA programming language typically executed by translating code line by line at runtime.
Common Confusions
Believing that compiled programs cannot be changed without recompiling.
Believing that compiled programs cannot be changed without recompiling. While compiled programs need recompilation to change the machine code, source code can always be edited and recompiled; some languages also support dynamic features.
Thinking interpreted programs always run slower than compiled ones.
Thinking interpreted programs always run slower than compiled ones. Interpreted programs often run slower, but modern techniques like just-in-time compilation can make them run nearly as fast as compiled programs.
Summary
Programs must be translated into machine code so computers can understand and run them.
Compilation translates the whole program before running, making execution fast but requiring waiting time upfront.
Interpretation translates code line by line during execution, allowing quick testing but slower running speed.