Il 0% ha trovato utile questo documento (0 voti)
5 visualizzazioni

Coding 1

IGCSE Pseudocode

Caricato da

randomsites7
Copyright
© © All Rights Reserved
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Il 0% ha trovato utile questo documento (0 voti)
5 visualizzazioni

Coding 1

IGCSE Pseudocode

Caricato da

randomsites7
Copyright
© © All Rights Reserved
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Sei sulla pagina 1/ 60

Cambridge iGCSE

Computer Science
Coding
1 – Design
2 – Pseudocode
Colour Coding

Main Topic Headings


Sub-headings
Key Words
Practice

Tips!
1 - Design
Structure designs

Structure diagrams can be used to show top-down design in a diagrammatic form.


Structure diagrams are hierarchical, showing how a computer system solution can be
divided into sub-systems with each level giving a more detailed breakdown.
If necessary, each sub-system can be further divided.
Consider the alarm app computer system for a smart phone; this could be divided into
three sub-systems:
• setting the alarm
• checking for the alarm time
• sounding the alarm.
These sub-systems could then be further sub-divided; a structure diagram makes the
process clearer.
Flowcharts

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

Flowcharts are drawn using standard flowchart symbols.


Begin / End

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

Pseudocode is a simple method of showing an algorithm.


It describes what the algorithm does by using English key words that are very similar
to those used in a high-level programming language.
Data items to be processed by the algorithm are given meaningful names in the same
way that variables and constants are in a high-level programming language.
However, pseudocode is not bound by the strict syntax rules of a programming
language.
It does what its name says, it pretends to be programming code!
To ensure that pseudocode is easily understandable by others it is useful to be
consistent in the way that it is written.
The pseudocode is easier to read, understand and write when:
• a non-proportional font is used throughout
• all keywords (words used to describe a specific action e.g. INPUT) are written in
capital letters
• all names given to data items and subroutines start with a capital letter
• where conditional and loop statements are used, repeated or selected
statements are indented by two spaces.
Assigned values

A value is assigned to an item/variable using the ⟵ operator.


The variable on the left of the ⟵ is assigned the value of the expression on the right.
The expression on the right can be a single value or several values combined with any
of the following mathematical operators.
Examples of pseudocode assignment statements:
Practice

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

• comparisons made by using comparison operators, where comparisons are


made from left to right, for example: A > B means ‘A is greater than B’
Comparison operators:
Have a look at the algorithm below that checks if a percentage mark is valid and
whether it is a pass or a fail.
This makes use of two IF statements; the second IF statement is part of the first ELSE
path.
This is called a nested IF.
a choice between several different values, such as: CASE OF … OTHERWISE …
ENDCASE

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

All types of loops can all perform the same task.


Example: Display 10 * (asterisks)

The FOR … TO … NEXT loop is the most efficient way


for a programmer to write this type of task as the
loop counter is automatically managed.
FOR …. TO …NEXT loops

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.

OUTPUT is used to display information either on a screen or printed on paper; it is


usually followed by a single value that is a string or a variable, or a list of values
separated by commas, for example:
Algorithms

An algorithm sets out the steps to complete a given task.


This is usually shown as a flowchart or pseudocode, so that the purpose of the task and
the processes needed to complete it are clear to those who study it.
The purpose of the following pseudocode is to output the alarm sound at the
appropriate time.
The processes are:
• waiting 10 seconds,
• getting the current time,
• checking the current time with the alarm time,
• and outputting the alarm sound when the times match.
Practice

Have a look at the flowchart and pseudocode:


• identify the purpose of the algorithm that they both represent
• identify the processes included in the algorithm.
What would be output if the numbers 7 and 18 were input?
Finding solutions

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

Totalling means keeping a total that values are added to.


For example, keeping a running total of the marks awarded to each student in a class.
Counting

Keeping a count of the number of times an action is performed is another standard


method.
For example, counting the number of students that were awarded a pass mark:
Counting is also used to count down until a certain value is reached, for example,
checking the number of items in stock in a supermarket:
Maximum, minimum and average

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

A search is used to check if a value is stored in a list, performed by systematically


working through the items in the list.
There are several standard search methods, but you only need to understand one
method for IGCSE Computer Science.
This is called a linear search, which inspects each item in a list in turn to see if the item
matches the value searched for.
For example, searching for a name in a class list of student names, where all the names
stored are different:
In this example, the search checks how many people chose ice cream as their favourite
dessert, where several values in the list can be the same.
Bubble sort

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

Validation is the automated checking by a program that data is reasonable before it is


accepted into a computer system.
When data is validated by a computer system, if the data is rejected a message should
be output explaining why the data was rejected and another opportunity given to
enter the data.
There are many different types of validation checks including:
• range checks
• length checks
• type checks
• presence checks
• format checks
• check digits.
Range check

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 length check checks either:


• that data contains an exact number of characters,
• Eg: a password must be exactly eight characters in length so that passwords
with seven or fewer characters or nine or more characters would be rejected,
for instance:
• or that the data entered is a reasonable number of characters,
• Eg: a family name could be between two and thirty characters inclusive so
that names with one character or thirty-one or more characters would be
rejected.
Type 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.

Potrebbero piacerti anche