Coding 1
Coding 1
Computer Science
Coding
1 – Design
2 – Pseudocode
Colour Coding
Tips!
1 - Design
Structure designs
A flowchart shows diagrammatically the steps required to complete a task and the
order that they are to be performed.
These steps, together with the order, are called an algorithm.
Flowcharts are an effective way to communicate how the algorithm that makes up a
system or sub-system works.
Example: Checking for the alarm time
Symbols
Terminator flowchart symbols are used at the beginning and end of each flowchart.
Process
Process flowchart symbols are used to show actions, for example, when values are
assigned to variables.
If a process has been defined elsewhere then the name of that process is shown.
Input and Output
The same flowchart symbol is used to show the input of data and output of
information.
Decision
Decision flowchart symbols are used to decide which action is to be taken next; these
can be used for selection and repetition/iteration.
There are always two outputs from a decision flowchart symbol.
Flow lines
Flowchart flow lines use arrows to show the direction of flow, which is usually, but not
always, top to bottom and left to right.
Example:
Tickets are sold for a concert at $20 each, if 10 tickets are
bought then the discount is 10%, if 20 tickets are bought
the discount is 20%.
No more than 25 tickets can be bought in a single
transaction.
This is flowchart showing an algorithm to calculate the
cost of buying a given number of tickets:
2 - Pseudocode
Pseudocode
What values will the following variables have after the assignments have been
completed?
Practice
What values will the following variables have after the assignments have been
completed?
Conditional statements
When different actions are performed by an algorithm according to the values of the
variables, conditional statements can be used to decide which action should be taken.
There are two types of conditional statement:
• a condition that can be true or false such as: IF … THEN … ELSE … ENDIF
• a choice between several different values, such as: CASE OF … OTHERWISE …
ENDCASE
a condition that can be true or false such as: IF … THEN … ELSE … ENDIF
For an IF condition the THEN path is followed if the condition is true, and the ELSE
path is followed if the condition is false.
There may or may not be an ELSE path.
The end of the statement is shown by ENDIF.
There are different ways that an IF condition can be set up:
• use of a Boolean variable that can have the value TRUE or FALSE
For a CASE statement the value of the variable decides the path to be taken.
Several values are usually specified.
OTHERWISE is the path taken for all other values.
The end of the statement is shown by ENDCASE.
Have a look at the algorithm below that specifies what happens if the value of Choice
is 1, 2, 3 or 4.
Iteration
When some actions performed as part of an algorithm need repeating this is called
iteration.
Loop structures are used to perform the iteration.
Pseudocode includes these three different types of loop structure:
• A set number of repetitions FOR … TO … NEXT
• A repetition, where the number of repeats is not
known, that is completed at least once: REPEAT … UNTIL
• A repetition, where the number of repeats is not
known, that may never be completed: WHILE … DO … ENDWHILE
A variable is set up, with a start value and an end value, this variable is incremented in
steps of one until the end value is reached and the iteration finishes.
The variable can be used within the loop so long as its value is not changed.
This type of loop is very useful for reading values into lists with a known length.
REPEAT … UNTIL loop
This loop structure is used when the number of repetitions/ iterations is not known,
and the actions are repeated UNTIL a given condition becomes true.
The actions in this loop are always completed at least once.
This is a post-condition loop as the test for exiting the loop is at the end of the loop.
WHILE … DO … ENDWHILE loop
This loop structure is used when the number of repetitions/iterations is not known,
and the actions are only repeated WHILE a given condition is true.
If the WHILE condition is untrue then the actions in this loop are never performed.
This is a pre-condition loop as the test for exiting the loop is at the beginning of the
loop.
The pseudocode for input and output statements
INPUT and OUTPUT are used for the entry of data and display of information.
Sometimes READ can be used instead of INPUT but this is usually used for reading
from files.
INPUT is used for data entry; it is usually followed by a variable where the data input
is stored, for example:
Also, PRINT can be used instead of OUTPUT if a hard copy is required.
The ability to repeat existing methods is very important in the design of algorithms;
when an algorithm is turned into a program the same methods may be repeated many
thousands of times.
For iGCSE, you need to be able to use and understand these standard methods used in
algorithms:
• Totalling
• Counting
• Finding maximum, minimum, and average (mean) values
• Searching using a linear search
• Sorting using a bubble sort.
Totalling
Finding the largest and smallest values in a list are two standard methods that are
frequently found in algorithms, for example, finding the highest and lowest mark
awarded to a class of students.
If the largest and smallest values are not known, an alternative method is to set the
maximum and minimum values to the first item in the list.
For example, using this method to find the highest and lowest mark awarded to a class
of students.
Calculating the average (mean) of all the values in a list is an extension of the totalling
method, for example, calculating the average mark for a class of students.
Linear search
Lists can be more useful if the items are sorted in a meaningful order.
For example, names could be sorted in alphabetical order, or temperatures could be
sorted in ascending or descending order.
There are several standard sorting methods available, but you only need to understand
one method for IGCSE.
This method of sorting is called a bubble sort.
Each element is compared with the next element and swapped if the elements are in
the wrong order, starting from the first element and finishing with next-to-last
element.
Once it reaches the end of the list, we can be sure that the last element is now in the
correct place.
However, other items in the list may still be out of order.
Each element in the list is compared again apart from the last one because we know
the final element is in the correct place.
This continues to repeat until there is only one element left to check or no swaps are
made.
For example, the bubble sort algorithm can be used to sort a list of ten temperatures
stored in the array, Temperature[], into ascending order.
It could be written in pseudocode as:
Validation
Different types of check may be used on the same piece of data; for example, an
examination mark could be checked for reasonableness by using a range check, a type
check and a presence check.
A range check checks that the value of a number is between an upper value and a
lower value.
For example, checking
that percentage marks are
between 0 and 100
inclusive:
Length check
A type check checks that the data entered is of a given data type
• Eg: that the number of brothers or sisters would be an integer (whole number).
Presence check
A presence check checks to ensure that some data has been entered and the value has
not been left blank, for example, an email address for an online transaction must be
completed.