Course 7.2Lesson 1 of 3
Pseudocode writes an algorithm in a precise, readable form that is not tied to one programming language. You learn the exam style of Cambridge IGCSE pseudocode: assignment with an arrow, INPUT and OUTPUT, IF and CASE, the three kinds of loop, MOD and DIV, and how to state the purpose of an algorithm and amend one.
Try the code and the simulations beside the lesson.
A recipe says "beat the eggs", not "move the fork in a circle 40 times". It is precise enough to follow, but not tied to one kitchen. Pseudocode is a recipe for a computer. It is precise, it reads like a program, and it does not belong to any one programming language.
In Paper 2 of Cambridge IGCSE Computer Science, questions show algorithms in one fixed style of pseudocode. You must read it and write it. The syllabus says precision is required: x > y is accepted, but "x is greater than y" is not. This lesson teaches the parts you need for algorithms. The next chapter, Programming in Pseudocode and Python, goes further with procedures, functions, arrays and files.
IF, WHILE, OUTPUT.NumberOfPlayers, Total. They use only letters and digits and start with a capital letter.Total ← 0 means "Total takes the value 0".THEN and ELSE are indented by only two spaces.// and go to the end of the line.OUTPUT "Enter the length and the width"
INPUT Length
INPUT Width
Area ← Length * Width
OUTPUT "Area: ", AreaOUTPUT can show several values separated by commas. Here it shows a message and then the value of Area. The arithmetic operators are +, -, *, / and ^ (a power). Two more work on whole numbers.
DIV(17, 5) returns : the whole number of times goes into .MOD(17, 5) returns : the remainder.The comparison operators are =, <, <=, >, >= and <>. The last one means "not equal to". The logical operators are AND, OR and NOT.
An IF statement chooses between two paths. Notice the layout: the condition on the IF line, then THEN and ELSE on their own lines, two spaces in.
IF Temp > 30
THEN
OUTPUT "Hot"
ELSE
OUTPUT "Mild"
ENDIFA CASE statement chooses one of several paths by the value of a variable. The cases are tested in order. OTHERWISE is used when no case matches.
INPUT Move
CASE OF Move
'L' : Position ← Position - 1
'R' : Position ← Position + 1
OTHERWISE OUTPUT "Use L or R"
ENDCASEFOR ... TO ... NEXT runs a fixed number of times. STEP changes the amount added each time, and it can be negative.WHILE ... DO ... ENDWHILE tests the condition before each pass. It may not run at all.REPEAT ... UNTIL tests the condition after each pass. It always runs at least once.INPUT Table
FOR Count ← 1 TO 5
OUTPUT Count, " x ", Table, " = ", Count * Table
NEXT CountWith the input this outputs five lines, from 1 x 7 = 7 to 5 x 7 = 35. Here is a WHILE loop. It is the example the syllabus itself gives.
INPUT Number
WHILE Number > 9 DO
Number ← Number - 9
ENDWHILE
OUTPUT NumberTo state the purpose of an algorithm, run it in your head with a few inputs and look at the pattern. The WHILE loop above takes away until or less is left. For it outputs , because . So its purpose is: "It outputs the remainder when the number is divided by , but gives instead of ."
To amend an algorithm, change only what the question asks, and keep the style. To make the times table run to , change FOR Count ← 1 TO 5 to FOR Count ← 1 TO 10.
The grey blocks marked pseudocode are written in the exam style and do not run. The Python box is the same algorithm in Python, and that one runs. The Python below runs the times table and the WHILE loop. In Python, range(1, 6) gives the numbers to , and MOD and DIV are written % and //. You met these in the Python Foundations book.
= for assignment. In pseudocode, assignment is ←. The sign = is a comparison.Score >= 50, not "Score is at least 50".MOD gives the remainder, DIV the whole-number part.← for assignment, // for comments.MOD(a, b) is the remainder and DIV(a, b) the whole-number quotient.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Score.DIV(29, 4) and MOD(29, 4) return?FOR Number ← 10 TO 1 STEP -3 give Number each time?IF Mark > 40.Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.