⬅ Previous Topic
How Computers Understand CodeNext Topic ⮕
Programming Paradigms⬅ Previous Topic
How Computers Understand CodeNext Topic ⮕
Programming ParadigmsWhen we write a program using any programming language, the computer doesn’t understand it directly. Computers only understand machine code—binary (0s and 1s). So, we need a translator to convert our human-readable code into machine-readable code.
This is where compilers and interpreters come in. Both are types of language translators that serve this purpose, but they work differently.
A compiler reads the entire program, translates it into machine code, and creates a separate executable file. This executable can be run directly by the computer without needing the compiler again.
Suppose you write a program to add two numbers:
START
READ A, B
SUM = A + B
PRINT SUM
END
When you use a compiler:
Why does a compiler show all the errors at once?
Because it processes the whole code in one go. It builds a full understanding of your code before running it, which allows it to find and report multiple issues in a single pass.
An interpreter translates and runs the program line-by-line. It reads each line, converts it to machine code, and executes it immediately before moving to the next.
PRINT "Enter number A"
READ A
PRINT "Enter number B"
READ B
SUM = A + B
PRINT "Sum is", SUM
If this is run using an interpreter:
Why is interpreting slower than compiling?
Because each time the program runs, the interpreter must re-read and re-translate each line. In contrast, compiled code runs directly since it's already translated.
Aspect | Compiler | Interpreter |
---|---|---|
Translation | Whole program at once | Line by line |
Error Reporting | All errors after compilation | Stops at the first error |
Speed | Faster execution (after compile) | Slower due to real-time translation |
Output | Creates an executable file | Does not create executable |
PROGRAM SumCalculator
READ A, B
RESULT = A + B
PRINT RESULT
END PROGRAM
Output:
(Compiler creates executable) User inputs: 10 and 20 30
PRINT "Enter first number"
READ A
PRINT "Enter second number"
READ B
RESULT = A + B
PRINT RESULT
Output:
Enter first number (Reads user input) Enter second number (Reads user input) 30
Is it possible for a language to use both?
Yes! Some languages are compiled to an intermediate form and then interpreted (e.g., bytecode interpreted by a virtual machine).
⬅ Previous Topic
How Computers Understand CodeNext Topic ⮕
Programming ParadigmsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.