OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2
Languages and IDEs
From readable code to binary. How translators work, and the tools that help you write and debug.
Paper 2 · Lesson 21
Les van Computing
In deze les zitten 22 slides, met interactieve quizzen en tekstslides.
OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2
Languages and IDEs
From readable code to binary. How translators work, and the tools that help you write and debug.
Paper 2 · Lesson 21
Lesson 21 of the J277 series and the last lesson of Block C (Paper 2, section 2.5 Programming languages and Integrated Development Environments).
Cover hologram made from a photo by Gabriel Heinzer / Unsplash.
Device-free: show the slide; pupils write the title and date. Image credit: Gabriel Heinzer / Unsplash (cover hologram)
00 · MISSION · BY THE END OF THIS LESSON
By the end of this lesson you can…
01 Explain the differences between high-level and low-level languages.
02 Compare how a compiler and an interpreter translate code.
03 Describe the tools in an IDE that help programmers find errors.
Read the three outcomes. J277 section 2.5 expects pupils to know the characteristics and purpose of high-level and low-level languages, why translators are needed, the characteristics of a compiler and an interpreter, and the common tools and facilities of an IDE.
Device-free: pupils copy the outcomes and tick each one at the end.
02 · CHECK · DO NOW (LESSON 20)
Warm up: robust programs
1 What does input validation do?
2 Name two validation checks.
3 What is erroneous test data?
4 A program runs but gives the wrong answer. Syntax error or logic error?
▮ TERMINAL // do_now_answers
1 checks input is sensible
before it is used
2 e.g. range check,
presence check
3 wrong data type, e.g. "ten"
for a number: reject it
4 a logic error
Retrieval from lesson 20. Give 4 minutes, then click to reveal the answers line by line.
1 Validation checks that input data is sensible and follows set rules before the program uses it. It cannot check that the data is actually correct. 2 Any two of: range check, presence check, length check, format check, type check. 3 Erroneous data is data of the wrong type, which the program should reject, for example "ten" when a number is expected. 4 A logic error: the program runs, but the output is wrong. A syntax error breaks the rules of the language, so the code will not run at all.
Today links back to this: an IDE helps find both kinds of error.
Device-free: questions on the board; mini whiteboards.
A form accepts ages 11 to 16. Which test value is boundary data?
13
16
25
ten
Answer: B, 16. Boundary data sits at the edge of the allowed range, so 11 and 16 are the boundary values.
13 is normal data (inside the range). 25 is invalid data: the right type, but outside the range, so it should be rejected. "ten" is erroneous data: the wrong data type.
Device-free: students hold up A, B, C or D cards. Image credit: Corinne Kutz / Unsplash
01 · LEARN · MISSION BRIEFING
Speak the machine's language
A CPU only understands machine code: binary instructions. So how does your code run?
LANGUAGES
High-level for people, low-level for the CPU.
TRANSLATORS
Compiler, interpreter and assembler.
THE IDE
One program with all the tools to write and debug code.
SYSTEM VOICE
System online. Today's mission: decode the languages of the machine. Learn how high-level code becomes machine code, how a compiler, an interpreter and an assembler do the job, and which tools in an IDE help you write and debug programs.
Click play for the system voice briefing. The words are also on the slide.
Quick hook: ask what language pupils write their programs in (Python, perhaps Scratch or C#). Then ask: does the CPU understand Python? It does not. Every program has to end up as machine code, and today is about how that happens.
Device-free: pupils write the three headings and one thing they already know under each.
01 · LEARN · HIGH-LEVEL AND LOW-LEVEL
Two levels of language
HIGH-LEVEL e.g. Python, C#, Java
+ close to English, easy to read and write
+ portable: runs on different types of CPU
+ one line can become many machine code instructions
– must be translated before the CPU can run it
LOW-LEVEL machine code and assembly
+ direct control of the hardware
+ can be fast and use little memory
– hard to read, write and debug
– written for one type of CPU only
Machine code is binary the CPU runs directly. Assembly uses short words (mnemonics) instead.
Click to reveal the points in each card.
High-level languages are written for people: English-like keywords, easy to read and maintain, and portable, because a translator turns them into machine code for whichever CPU is used. One high-level statement usually becomes several machine code instructions.
Low-level languages are close to the hardware. Machine code is binary, the only thing a CPU can execute directly. Assembly language replaces the binary with mnemonics such as ADD and STA, and each assembly instruction usually matches one machine code instruction. Low-level code is specific to one type of processor, but gives direct control of the hardware, which is why it is used in some embedded systems and device drivers.
Misconception: "low-level" does not mean "worse" or "easier". It means closer to the hardware.
Device-free: pupils draw the two columns and add an example of each.
01 · LEARN · ONE TASK, THREE LEVELS
Add a and b, store in total
▮ HIGH-LEVEL // add.py
total = a + b
1 readable line
▮ ASSEMBLY // add.asm
LDA a
ADD b
STA total
load, add, store
▮ MACHINE CODE // add.bin
0001 0100
0010 0101
0011 0110
what the CPU runs
Illustration only: the assembly and binary here are made up for this example. Real codes depend on the CPU, and you will not be asked to write assembly code.
The same job at three levels. In Python or OCR Exam Reference Language it is one line. In assembly it takes three instructions: load a into the accumulator (LDA), add b to it (ADD), store the result in total (STA). In machine code each of those becomes one binary instruction.
Be clear with pupils: the mnemonics are in the style of Little Man Computer, and the binary opcodes and addresses are invented for this slide. Real machine code depends on the processor. J277 does not ask pupils to write or read assembly code; they need to know what machine code and assembly language are and how they differ from high-level languages.
Point out the pattern: one assembly instruction matches one machine code instruction, while one high-level line becomes several.
Device-free: pupils copy the three versions side by side and label which one the CPU can run directly.
01 · LEARN · TRANSLATORS
Why we need translators
The CPU only runs machine code, so every other language has to be translated first.
COMPILER
High-level code: whole program at once, makes an executable.
INTERPRETER
High-level code: translates and runs one line at a time.
ASSEMBLER
Assembly code into machine code.
SYSTEM VOICE · KEY DEFINITION
A compiler translates the whole program in one go and produces an executable file. An interpreter translates and runs the code one line at a time, and stops at the first error it finds.
A translator turns source code into machine code the CPU can execute. Compilers and interpreters translate high-level languages; an assembler translates assembly language. Knowing what an assembler does is useful background: OCR's exam questions on translators focus mainly on compilers and interpreters.
Play the key definition. Then ask: if an interpreter stops at the first error, what happens to the lines after it? (They are not run until the error is fixed.)
Device-free: pupils write one sentence for each translator.
01 · LEARN · COMPARE
Compiler or interpreter?
▮ TERMINAL // compare_translators
| COMPILER | INTERPRETER
---------------+------------------------+--------------------
translates | whole program at once | one line at a time
errors | listed after the whole | stops at the first
| program is checked | error it finds
executable | yes, a file is made | no file is made
run speed | fast once compiled | slower
needed to run? | no | yes, every time
best for | a finished program | writing and testing
Click to reveal the table row by row. These are the points examiners look for.
Translating: a compiler translates all the source code in one go; an interpreter translates and executes one line at a time. Errors: a compiler reports all the errors it finds after checking the whole program, so nothing runs until they are fixed; an interpreter stops as soon as it reaches a line with an error, so you can see exactly where the problem is. Executable: a compiler produces an executable file; an interpreter does not. Speed: a compiled program runs quickly because it is already in machine code; interpreted code is slower because it is translated every time it runs. Run time: the compiled program can run without the compiler, so users do not need it (and do not see the source code); interpreted code needs the interpreter every time.
When to use which: interpreters suit writing and testing code; compilers suit a finished program you want to give to users.
Device-free: pupils copy the table with the two columns.
01 · LEARN · THE IDE EDITOR
Tools that help you write
IDE = Integrated Development Environment: one program with all the tools to write, run and debug code.
Line numbers find the line an error is on.
Syntax highlighting colours keywords, so typos stand out.
Auto-indent indents code inside loops and ifs.
Auto-complete suggests names as you type.
Click to reveal each feature. The picture is a real code editor: point to the line numbers down the left, the different colours for keywords, strings and names (syntax highlighting), and the way code inside each block is indented.
Line numbers: error messages give a line number, so you can go straight to it. Syntax highlighting: keywords, strings and variables appear in different colours, so a misspelt keyword or a missing quote mark is easy to spot. Auto-indent: the editor indents code inside loops and selection automatically, which keeps the structure clear (and in Python, the indentation matters). Auto-complete: the editor suggests keywords and variable names as you type, which saves time and avoids spelling mistakes.
Photo: Pankaj Patel / Unsplash.
Device-free: pupils sketch an editor window and label the four features. Image credit: Pankaj Patel / Unsplash
01 · LEARN · FINDING ERRORS
Tools that help you debug
ERROR DIAGNOSTICS
Highlights errors, says what is wrong and which line it is on.
BREAKPOINTS AND STEPPING
Pause at a chosen line, then run one line at a time and watch the variables change.
RUN-TIME ENVIRONMENT
Run and test the program inside the IDE and see its output.
TRANSLATOR
A built-in compiler or interpreter turns your code into something the CPU can run.
The editor helps you write code; these tools help you find and fix errors.
Error diagnostics: the IDE reports errors (often as you type, or when you run the code), explains what is wrong and gives the line number. This mostly helps with syntax errors. Breakpoints and stepping: a breakpoint pauses the program at a chosen line; stepping then runs it one line at a time while you watch the values of the variables. This is how to track down logic errors, which give no error message. Run-time environment: lets the program run inside the IDE so it can be tested, with its output (and any run-time errors) shown. Translator: the IDE includes a compiler or interpreter.
Link to lesson 20: syntax errors are found with error diagnostics; logic errors with breakpoints, stepping and a trace table.
Device-free: pupils match each tool to the type of error it helps find.
Compiler or interpreter?
COMPILER
INTERPRETER
Stops at the first error it finds
Produces an executable file
Translates the whole program at once
Needed every time the program runs
Translates and runs one line at a time
Not needed to run the finished program
Answers. Compiler: produces an executable file; translates the whole program at once; not needed to run the finished program. Interpreter: stops at the first error it finds; needed every time the program runs; translates and runs one line at a time.
Device-free: pupils draw two columns and write each statement in the right one.
Which translator turns assembly language into machine code?
Compiler
Interpreter
Assembler
Debugger
Answer: C, assembler. It translates assembly language mnemonics (such as ADD) into the machine code the processor in the picture can execute.
Compilers and interpreters translate high-level languages. A debugger is not a translator: it is a tool for finding errors.
Device-free: students hold up A, B, C or D cards. Image credit: Brecht Corbeel / Unsplash
Which IDE tool pauses a program at a chosen line so you can check the variables?
Breakpoint
Auto-indent
Line numbers
Auto-complete
Answer: A, breakpoint. The program runs until it reaches the breakpoint, then pauses so the programmer can inspect the values of variables, often then stepping through one line at a time.
The other three are editor features: they help you write code, but do not pause a running program. The line numbers in the picture are still useful: you set a breakpoint by clicking next to a line number in most IDEs.
Device-free: students hold up A, B, C or D cards. Image credit: Chris Ried / Unsplash
03 · DEBUG · SPOT THE MISTAKE
One line in Tomasz's notes is wrong
▮ TERMINAL // tomasz_translators.txt
1 machine code: binary the CPU runs
2 assembler: assembly to machine code
3 compiler: makes an executable file
4 interpreter: runs faster than compiled code
5 high-level: portable between CPUs
Line 4. Interpreted code runs SLOWER than compiled code, because each line is translated again every time the program runs.
Check each line against what you know. Then tap the marker.
Tip: think about how each translator works, line by line or all at once.
Give a minute to find the mistake before tapping the marker.
Answer: line 4. A compiled program is already in machine code, so it runs quickly. An interpreter has to translate each line as the program runs, every time, so interpreted code runs more slowly. Lines 1, 2, 3 and 5 are correct.
Device-free: copy the five lines onto the board; pupils write the wrong line number and the correction.
Fix the errors and compile again
Write the source code in the editor
The compiler checks the whole program
The executable runs without the compiler
It lists the syntax errors it found
The compiler produces an executable file
Put the steps from source code to a running compiled program in order.
Correct order: 1 write the source code in the editor; 2 the compiler checks the whole program; 3 it lists the syntax errors it found; 4 fix the errors and compile again; 5 the compiler produces an executable file; 6 the executable runs without the compiler.
Ask: how would this be different with an interpreter? (No executable; the code runs line by line and stops at the first error; the interpreter is needed every time.)
Device-free: pupils write the six steps on sticky notes and order them on the desk.
Complete the summary.
Machine code is binary that the CPU runs directly. An assembler translates assembly language into machine code. A compiler translates the whole program at once and makes an executable file. An interpreter translates and runs one line at a time. In an IDE, a breakpoint pauses a program at a chosen line.
Answers in order: CPU, assembler, compiler, interpreter, breakpoint. Distractors: browser, spreadsheet, monitor.
Device-free: paragraph on the board with gaps and the word bank; pupils write the five missing words.
EXAM · Ama can translate her program with a compiler or an interpreter. Compare the two. [4 marks]
Exam-style question, 4 marks. Give 6 minutes. The model answer is on the next slide.
Mark scheme: 1 mark per valid point, up to 4. A compare question rewards both sides, so the best answers pair each compiler point with the matching interpreter point. Compiler translates the whole program at once / interpreter translates one line at a time. Compiler produces an executable file / interpreter does not. Compiled program runs faster / interpreted program runs more slowly as it is translated every time. Compiler reports all errors after translating the whole program / interpreter stops at the first error it finds. Compiler is not needed when the program runs / interpreter is needed every time it runs.
Do not award "a compiler is faster" without saying what is faster (the finished program runs faster; compiling itself can take a while).
Device-free: pupils answer on paper, then peer-mark with the next slide.
04 · EXAM · MODEL ANSWER
Pair each point
▮ TERMINAL // model_answer
1 A compiler translates the whole
program at once, an interpreter
one line at a time.
2 A compiler makes an executable
file; an interpreter does not.
3 Compiled code runs faster.
4 An interpreter stops at the first
error; a compiler lists them all.
MARK SCHEME
1 mark per point, up to 4
whole program / line by line
executable / no executable
faster / slower to run
all errors listed / stops at first
not needed / needed at run time
Click to reveal the model answer point by point. Each point names BOTH translators, which is what "compare" asks for.
Any four of the pairs in the mark scheme score full marks. Common mistakes: describing only the compiler; saying an interpreter "makes" an executable; saying "a compiler is faster" without saying it is the compiled program that runs faster.
Device-free: pupils check their own answer against the mark scheme and correct it in a different colour.
02 · CHECK · BLOCK C QUICK-FIRE
End of Block C
1 What is abstraction? (L15)
2 What must be true before a binary search? (L17)
3 Which sort splits a list into single items, then merges them? (L18)
4 What is 1 AND 0? (L19)
▮ TERMINAL // block_c_recap
1 removing detail that is
not needed
2 the list must be sorted
3 merge sort
4 0 (both must be 1)
A quick-fire recap of Block C before the exit poll. Give 3 minutes, then click to reveal the answers line by line.
1 Abstraction means removing unnecessary detail so you can focus on what matters for the problem. 2 The data must be sorted (in order). 3 Merge sort: it divides the list until each part has one item, then merges the parts back together in order. 4 0: AND only outputs 1 when both inputs are 1.
Any wrong answers show which Block C lesson to revisit before the end-of-block test.
Device-free: mini whiteboards, one answer at a time.
How sure are you about translators and IDEs?
Very: I can compare them
Fairly: I mix some points up
Not yet: I need another go
Anonymous exit poll. If many pupils mix up the points, revisit the comparison table and the drag-and-drop at the start of the next lesson.
Device-free: thumbs up, sideways or down.
05 · NEXT · KEEP GOING
Keep going
Isaac Computer Science
Free online lessons and questions. Look for the translators and IDEs topics.
BBC Bitesize
GCSE Computer Science, OCR: revise programming languages and IDEs.
Try it
Open your IDE, set a breakpoint in a loop, and step through it watching a variable.
Block C complete. Next lesson starts Block D: variables, data types, operators and casting.
Block C is finished. Point pupils to the places to practise: Isaac Computer Science and BBC Bitesize are free.
Try it: in IDLE, Thonny, VS Code or the school's IDE, set a breakpoint inside a short loop and step through it, watching a variable change on each pass. If your IDE has no debugger, print the variable on each pass instead.
Device-free: pupils write a short comparison of a compiler and an interpreter from memory, to bring to lesson 22.