Information Technology Topic

Pseudocode: Input, Output and Selection

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. Read and Input take a value in, Store puts a value into a variable, and Write, Output and Display show a result; pseudocode is English-like, not a program, and has no single fixed standard
  2. If-then runs its lines only when the condition is true; If-then-else runs exactly one of its two parts; conditions use < > = <= >= <> and the boundary value decides > or >=
  3. The syllabus keeps pseudocode free of nested conditionals, so a problem with more than two outcomes uses separate If statements one after another

Section 1What pseudocode is

Pseudocode is a way of writing an algorithm in English-like statements. It looks a little like code, but it is not code, because no computer can run it. Its job is to plan the steps and to explain them to other people. There is no single official pseudocode. Books and teachers write it a little differently, so the aim is to be clear and consistent. This topic uses the keywords the syllabus names: Read, Input, Store, Write, Output, Display, If-then and If-then-else. (The next topic covers the For loop and the While loop.) Every pseudocode line should say exactly one thing, and the lines run from top to bottom unless an If decides otherwise. Indent the lines that belong inside an If, so the structure can be seen at a glance.

Section 2Getting values in: Read, Input and Store

Read and Input do the same job: they take a value from the user (usually typed on the keyboard) and put it into a variable. Read price means 'get a value and keep it in the variable called price'. A variable is a named storage area, and its value can change as the algorithm runs. Store puts a value into a variable. Store 100 in total puts 100 into total. Store total + 50 in total works out total + 50 first, then puts the answer back in total, so total is now 150. Easy to mix up: Store replaces the old value, it does not add to it by itself. After Store 5 in x, Store x + 3 in x and Store x * 2 in x, x holds 16: 5, then 8, then 16. The line Store x + 3 in x is not an equation. It is an instruction. (Pascal, the language used later in the course, writes the same idea with :=, and keeps = for comparing.)

Section 3Showing results: Write, Output and Display

Write, Output and Display also do the same job as each other: they show something to the user. A question will use one of them, so know that they mean the same. Words inside quotation marks are shown exactly as they are. A variable name without quotation marks shows the value stored in the variable. Store 25 in price Display "price" This displays the word price. Without the quotation marks, Display price would show 25. You can show a message and a value together, separated by a comma: Display "Total:", total shows Total: followed by the value of total. For total = 150 the screen shows Total: 150. Watch out: a variable must have a value before you use it. In the lines below, total is displayed before anything has been stored in it, so the algorithm fails at the second line: Read a Display total Store a * 2 in total

Section 4Input, process, output in order

Many algorithms follow the pattern input, process, output. Read the values, do the calculation with Store, then Display the answer. The order of the lines matters, because each line uses what the earlier lines stored. Here is the average of two marks: Read a Read b Store (a + b) / 2 in average Display average With 60 and 75, the line Store (a + b) / 2 in average works out (60 + 75) / 2 = 135 / 2 = 67.5. Brackets matter: without them, a + b / 2 would work out b / 2 first. Here's the trick: write the three parts as a plan first. Inputs: a and b. Process: add and halve. Output: average. Then turn each part into lines.

Section 5If-then: do it only when the condition is true

If-then makes a choice. If the condition is true, the indented lines run. If it is false, they are skipped and the algorithm carries on after End If. The condition compares two values with a relational operator: < less than, > greater than, = equal to, <= less than or equal to, >= greater than or equal to, <> not equal to. A condition always works out as true or false. Read hours Read rate Store hours * rate in pay If hours > 40 then Store pay + 2000 in pay End If Display pay This gives 2000 extra when more than 40 hours are worked. With 45 hours at a rate of 900, pay is 40500 and then 42500. With exactly 40 hours the condition 40 > 40 is false, so the extra is skipped and pay stays at 36000. Watch out: the boundary. > leaves the limit out and >= includes it. Test the boundary value every time.

Section 6If-then-else: exactly one of two parts

If-then-else gives the algorithm two paths. When the condition is true, the part after then runs. When it is false, the part after Else runs. Exactly one of the two parts runs, never both and never neither. Read age If age <= 12 then Store 100 in fare Else Store 200 in fare End If Display "Fare:", fare For an age of 12 the condition is true, fare is 100 and the screen shows Fare: 100. For 13 it is false, fare is 200 and the screen shows Fare: 200. The Display line is outside the If, so it runs whichever path was taken. Another example: Read mark / If mark >= 50 then / Display "Pass" / Else / Display "Fail" / End If. Mark 50 shows Pass and mark 49 shows Fail. Easy to mix up: If-then has a path only for true. If-then-else has a path for both true and false.

Section 7More than two outcomes, and no nesting

The syllabus keeps pseudocode questions free of nested conditionals: an If inside another If is not asked. When you need more than two outcomes, write separate If statements one after the other. Finding the largest of three numbers is a good case: Read a Read b Read c Store a in largest If b > largest then Store b in largest End If If c > largest then Store c in largest End If Display largest Start by assuming a is the largest. Then each If asks whether the next number beats the current largest and, if so, replaces it. For 14, 29 and 22, largest goes 14, then 29, and stays 29. The If statements follow one another and none sits inside another. Watch out: separate If statements are all tested, so more than one can run. With the lines Read n / If n > 5 then / Display "A" / End If / If n > 8 then / Display "B" / End If, an n of 9 shows both A and B, because both conditions are true. To combine two tests in one condition, join them with AND (both true) or OR (at least one true), or reverse a test with NOT. For example mark >= 0 AND mark <= 100 is true only for marks from 0 to 100.

Examples

A bus company in Kingston uses this pseudocode to find a fare: Read age If age <= 12 then Store 100 in fare Else Store 200 in fare End If Display "Fare:", fare (a) State what is displayed when the age entered is 12, 13 and 40. (b) State which part of the If-then-else runs for each age. (c) Explain why the Display line is not indented under Else.

(a) For 12 the condition age <= 12 is true, so fare is 100 and the screen shows Fare: 100. For 13 the condition is false, so fare is 200 and the screen shows Fare: 200. For 40 it is also false, so the screen shows Fare: 200. (b) The then part (Store 100 in fare) runs for 12. The Else part (Store 200 in fare) runs for 13 and 40. Exactly one part runs each time. (c) The Display line has to run whichever path was taken, so it comes after End If, outside the If-then-else. If it were indented inside the Else part, it would only run for ages over 12 and a child would see no fare.

A Mandeville student writes this algorithm to find the largest of three numbers. It uses separate If statements, because nested conditionals are not asked: Read a Read b Read c Store a in largest If b > largest then Store b in largest End If If c > largest then Store c in largest End If Display largest (a) Trace the algorithm for the numbers 14, 29 and 22, showing the value of largest after each Store. (b) State what is displayed when the numbers are 31, 8 and 5. (c) Explain why both If statements are tested every time.

(a) Store a in largest makes largest 14. The first If asks 29 > 14, which is true, so largest becomes 29. The second If asks 22 > 29, which is false, so largest stays 29. Display largest shows 29. (b) largest starts at 31. 8 > 31 is false and 5 > 31 is false, so neither replacement runs. The value displayed is 31, the first number. (c) The two If statements follow one another and are separate, not joined by Else. Each one is tested whatever happened in the other. That is why each number gets its turn to beat the current largest.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from