75% developed

GCSE Computer Science/Pseudocode

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Pseudocode is a non-language-specific way of writing code. It is used during the design phase of a project as a quick way of devising algorithms before the language to be used is known and without needing to spend too much time using the exact syntax correct. Once pseudocode algorithms have been written it should then be easier to use this to help write the program code. Pseudocode is a structured way of writing algorithms which looks very much like program code.

Since pseudocode is an informal way of writing code there are different ways of writing it even at GCSE level. You should be aware both of what pseudo you are expected to be able to read and how you should write code for your course. Also you should be aware of whether or not a pseudocode fact sheet is provided.

  • AQA: understand their pseudocode; use any reasonable pseudocode to answer exam question; no pseudocode fact sheet in exam.

Defining Variables[edit | edit source]

When devising an algorithm the programmer will need to use variables and assign values to them. Many languages require a variable to be defined and given a data type before use.

DEFINE x AS integer

The five basic data types that can be used here include:

  • Char
  • String
  • Integer
  • Real
  • Boolean

Variable Assignments[edit | edit source]

Specification link

Understand and use pseudocode for assignment, using ← - 2016 CIE Syllabus p15

Assigning a value to a variable is indicated in pseudocode using an arrow symbol (←). The arrow points from the value being assigned towards the variable it is being assigned to.

The following line of pseudocode should be read as 'a becomes equal to 34'.

a ← 34

Totalling and Counting[edit | edit source]

Specification link

Understand and use pseudocode, using the following commands and statements:
- totalling (e.g. Sum ← Sum + Number)
- counting (e.g. Count ← Count + 1) - 2016 CIE Syllabus p15

The assignment operator can also be used when finding totals, as shown in the following example where x becomes equal to a plus b.

x ← a + b

Similarly we can also use the assignment operator for counting, by assigning a variable to become equal to the value of itself plus 1.

x ← x + 1

Input and Output[edit | edit source]

Specification link

Understand and use pseudocode, using the following commands and statements:
- INPUT and OUTPUT (e.g. READ and PRINT) - 2016 CIE Syllabus p15

Output is the act or returning some data to the user of the program. This could be in the form of a written message, a numerical value, an image, video or sound. This is shown in pseudocode by writing OUTPUT followed by what the output will be. The example below shows how we would output the message 'Hello World':

    OUTPUT 'Hello World'

Sometimes the word PRINT may be used instead of OUTPUT, as in the following example:

    PRINT 'Hello World'

Input on the other hand is when we expect some value to be provided by the user. The following example will output a message asking the user to input their name and assign it to a variable called 'name'.

    PRINT 'What is your name?'
    INPUT name

The word READ can also be used instead of INPUT.

    PRINT 'What is your name?'
    READ name

Selection[edit | edit source]

Specification link

Understand and use pseudocode, using the following conditional statements:

IF … THEN … ELSE … ENDIF
CASE … OF … OTHERWISE … ENDCASE

2016 CIE Syllabus p15

The use of selection allows decisions to be made within an algorithm at run-time based on certain conditions.

IF THEN ELSE[edit | edit source]

An IF statement starts with a condition which is tested. If the condition evaluates as TRUE then the THEN code block will be run. If the condition evaluates as false then the ELSE block will run. In the following example "Hello" will be printed if x = 1, otherwise "Good night" will be printed.

IF x = 1 THEN
    print "Hello"
ELSEIF
    print "Good night"
ENDIF

Often ELSE IF is used when there is more than one condition to check. The following example would print "Hello" if x = 1, or "How are you?" if x = 2 or will otherwise print "Goodbye".

IF x = 1 THEN
    print "Hello"
ELSE IF x = 2 THEN
    print "How are you?"
ELSE
    print "Goodbye"
ENDIF

CASE OF OTHERWISE ENDCASE[edit | edit source]

This structure is used when there are many possible outcomes to a condition. For instance, to extend the above example, suppose you wanted the program to print a different message depending on the value of a variable, xː

CASE x OF
    1 : PRINT "Hello"
    2 : PRINT "How are you?"
    3 : PRINT "I am fine"
    4 : PRINT "Have a good day!"

OTHERWISE
    PRINT "Goodbye"
ENDCASE

In this example if x = 1 then we print "Hello", if x =2 then "How are you?", if x = 3 then "I am fine." or if x = 4 then we print "Have a good dayǃ". If x is equal to anything else then we print "Goodbye".

Iteration[edit | edit source]

Specification link

understand and use pseudocode, using the following loop structures:

FOR … TO … NEXT
REPEAT … UNTIL
WHILE … DO … ENDWHILE

2016 CIE Syllabus p15

Iteration is used when we need to repeat a block of code. We can make a code block repeat either for a given number of repetitions or continuously either for as long as a condition continues to be true or until a condition becomes true.

FOR TO NEXT[edit | edit source]

The FOR loop is used to repeat code for a given number of repetitions. We specify a counter variable and set it to an initial value and then specify an end value. After every iteration of the loop the counter variable is automatically incremented by 1. In the following example x starts at 1 and increases by 1 every time the code repeats until it reaches a value of 10, then the loop will terminate.

FOR x = 1 TO 10
    print x
NEXT

REPEAT UNTIL[edit | edit source]

A REPEAT loop will repeat the code block until the given condition is true. The condition is not checked until after the code has run once, so regardless of whether the condition is true or not the code will always run at least once. The following example will continue to take user input until the user inputs a value less than 10. This structure is often used for validation checks.

REPEAT
     INPUT x
UNTIL x < 10

WHILE DO ENDWHILE[edit | edit source]

In a WHILE loop the code block will run, and continue to run, until the given condition is no longer true. A WHILE loop is similar to the REPEAT loop in that it decides when to terminate based on a condition. However the while loop checks the condition prior to running the first time. If the condition is not true then the code block will never run.

The below example is also used for input validation.

INPUT x
WHILE x < 10
    INPUT x
ENDWHILE