Skip to content

2.5.1 Languages

GCSE · Paper 2 · Computational thinking, algorithms and programming · Section 2.5 · 18 flashcards · 10 practice questions

Questions

2.5.1 Languages

Programs are written in programming languages. The computer's processor cannot understand most of them directly. This topic explains the two types of language, why a translator is needed, and how the two kinds of translator work.

High-level and low-level languages

High-level languages

Definition
A high-level language is a programming language that uses English-like words and is easy for humans to read, write and understand.

Examples are Python, Java, C# and the OCR Exam Reference Language you use in this course. One line of a high-level program, such as print("Hello"), may need many instructions from the processor.

Features of high-level languages:

  • •
    Use words and symbols that people understand, such as if, while and print.
  • •
    Not tied to one type of computer. The same program can usually run on different processors, as long as there is a translator for each.
  • •
    Have built-in features such as loops, functions and string handling, which make programs quicker to write.
  • •
    Easier to test and to fix because the code is readable.

Low-level languages

Definition
A low-level language is a programming language that is close to the instructions that the computer's processor carries out. It is harder for people to read.

The most important low-level language to know is machine code. This is the binary instructions (1s and 0s) that the processor runs directly. A low-level language gives the programmer control of the hardware, such as the exact memory used.

Features of low-level languages:

  • •
    Each instruction is very basic and often does one small job.
  • •
    Written for one type of processor. A program for one type of processor does not work on another type.
  • •
    Can run very fast and use little memory because the programmer has detailed control.
  • •
    Hard to read, write and fix, so programs take much longer to write.

Comparing the two

High-levelLow-level
ReadabilityEasy for humans to readHard to read
Time to writeQuickerSlower
Needs translatingYes, to machine codeMachine code does not.
Works on different computersUsually yesNo, specific to one processor
Control of hardwareLess controlVery detailed control
Speed and memory useOften slower and largerOften faster and smaller

Purpose of each

High-level languages are used for most software: websites, apps, games and business systems. Low-level code is used where speed, size or direct control of the hardware matter, for example in the small programs inside machines and devices (embedded systems) and in parts of an operating system.

Why translators are needed

The processor can only run machine code. A program written in a high-level language must be turned into machine code before the computer can run it.

Definition
A translator is a program that converts code written in one language into machine code that the processor can run.

The two types of translator you must know are the compiler and the interpreter. The code a programmer writes is called the source code. The machine code a compiler produces is called the object code (also called the executable).

Compiler

Definition
A compiler translates the whole of the source code into machine code in one go, before the program is run. It produces a separate file of machine code (an executable).

How it works:

  • 1.
    The programmer writes the program in a high-level language.
  • 2.
    The compiler checks and translates all of the code.
  • 3.
    If there are errors, the compiler reports them and does not produce the finished program.
  • 4.
    If there are no errors, an executable file is made. This file can be run again and again without the compiler or the source code.
Pros & Cons
Benefits of a compilerDrawbacks of a compiler
The finished program runs fast because it is already machine codeThe whole program is compiled, so there is a delay before you can run it
The program can be run without the compiler or the source codeErrors are reported only after the whole program has been translated, so there may be a long list
The source code is hidden. People get only the executable and cannot easily copy or change itThe machine code only works on one type of processor, so it must be compiled again for a different one

Interpreter

Definition
An interpreter translates the source code one line at a time and runs each line straight away. It does not make a separate file of machine code.

How it works:

  • 1.
    The interpreter reads the first line, translates it and runs it.
  • 2.
    It then moves on to the next line, and so on.
  • 3.
    If it finds an error, it stops at that line and reports it.
Pros & Cons
Benefits of an interpreterDrawbacks of an interpreter
Good for testing: you can run the program at once and a mistake is reported at the line where it happensThe program runs more slowly because each line must be translated every time it is run
Easy to fix a mistake and run again straight awayThe interpreter and the source code must be present to run the program, so the code is not hidden
Can run on any type of computer that has the right interpreterErrors later in the program are found only when that line is reached
Key Point
Compiler: translates everything first, then the program runs. Interpreter: translates and runs one line at a time.

Comparing compiler and interpreter

CompilerInterpreter
TranslatesThe whole program at onceOne line at a time
OutputA separate machine code fileNo separate file
Speed when program runsFastSlower
ErrorsShown after translating the whole programReported at the line where the program stops
Source code needed to runNoYes
Best forFinished programs that will be given to usersWriting and testing a program
Worked Example
A student is learning to program and keeps making small changes to test them. Which translator is best?

An interpreter. The student can run the program straight away after each change, and the error is shown at the line where the program stops, so mistakes are easier to find.

A company wants to sell a finished game and does not want customers to see its code. Which is best?

A compiler. The game is turned into an executable that runs fast, and customers receive only the machine code, not the source code.
Warning
A program can be translated by a compiler or an interpreter. It is not both at once. Do not say that an interpreter makes an executable file or that a compiler runs the program line by line.

Exam technique

  • •
    State and give: a short fact. "State one feature of a high-level language."
  • •
    Describe: say how something works. "Describe how an interpreter translates a program" needs one line at a time, run straight away, stop at an error.
  • •
    Explain: give a reason. "A translator is needed because the processor can only run machine code."
  • •
    Compare: write about both, using words such as "whereas" and "however". Give a point for each, for example "A compiler translates all the code at once, whereas an interpreter translates one line at a time."
  • •
    Recommend or choose for a scenario: pick one and give reasons that match the facts in the scenario. If you are asked for a drawback, give one.
  • •
    Speed, size and code readability are good points for low-level versus high-level answers. Hiding source code and needing the translator are good points for compiler versus interpreter.