Unit 1 · Basics of Computer Programming

1.3.3Self-check and practice

Self-check questions

Write your answers first, then open the answers to check them.

  1. Why can pseudocode not be run on a computer, and why is it still useful?
  2. What is the difference between a WHILE loop and a REPEAT ... UNTIL loop?
  3. In Example 4, what is the sentinel value, and why is the first READ placed before the loop?
Show answers
  1. It is not a real programming language: it has no strict syntax and no compiler or interpreter, so a computer cannot run it. It is still useful because you can plan the logic quickly in English-like steps that anyone can read, and then translate it into any programming language.

    • WHILE tests the condition before each pass (pre-test). The body may run zero times, and it repeats while the condition is true.
    • REPEAT ... UNTIL tests the condition after each pass (post-test). The body always runs at least once, and it repeats until the condition becomes true.
  2. The sentinel is 0: a special value that means "stop" and is not part of the data. The first READ before the loop gives x a value to test. The READ at the end of the body fetches the next value before the condition is tested again. If the first value is 0, the body never runs and the total is 0.

Practice exercises

  1. Write pseudocode to compute the factorial of N.
    Hint
    Pseudocode
    READ n
    fact = 1
    FOR i = 1 TO n DO
      fact = fact * i
    END FOR
    PRINT fact
  2. Write pseudocode to print the first N Fibonacci numbers.
  3. Write pseudocode to count how many vowels are in a word.
  4. Write pseudocode to find the largest value in a list of N numbers.
    Hint

    READ the first value into largest. Loop through the remaining N − 1 values; if a value is greater than largest, replace it.

  5. Convert your flowchart from exercise 1.2 (question 5) into pseudocode.