- [Craig] In this video, we're going to consider how to produce algorithms using pseudocode and flow diagrams.
(uplifting piano jingle)
So, the specification states that in the exam, you need to be able to create, interpret, correct and refine algorithms
using pseudocode, flowcharts, the OCR exam reference language or high-level programming code.
A flow diagram, often referred to as a flowchart, is just one way of representing a sequence of steps in an algorithm.
Here, we can see that we start at the top,
we input a number from the keyboard,
we input a second number from the keyboard,
we ask is number one greater than number two, and if it is, then we follow one branch,
and if it's not, we follow another.
Then there's different outputs depending on the outcome of that decision before the program comes to a stop.
We can represent that algorithm using what's called pseudocode.
Pseudocode is like a halfway between English language and actual programming code in a programming language.
It looks a little bit like a programming language but doesn't follow any specific syntax for one given language.
Instead, it's more generic code that could easily be applied into
any programming language that a programmer wants to use.
So, in this example, we're inputting our two numbers,
and we're saying if number one is greater than number two,
then print the largest number is number one.
Otherwise, print the largest number is number two
and then end.
So, it's exactly the same algorithm that we saw in the flowchart, it's just represented in a different way.
As well as creating an algorithm from scratch in the exam,
you might be presented with one already in the form of a flowchart or pseudocode, etc.,
and then be asked to refine it somehow.
So here, we see our previous flowchart from an algorithm to output the largest of two numbers entered by the user,
and we're being asked to refine it so it will now output the largest of three numbers entered by the user.
You would first need to identify the areas of the existing algorithm which need modifying.
Well, we would clearly need to modify this top section to take in an extra input from the user,
which you can see we've done here,
and we would also need to modify this bottom section.
Now, this will require a little more thought, so why don't you pause the video and see if you can
sketch out with a pen and paper how you would alter the bottom part of this flow diagram?
And here, you can see our refinement.
So, we've still got the is number one greater than number two, but then if the answer's yes or no, we're performing another check,
and depending on the result of that, we're either outputting number one, two or three is the largest.
Of course, in the exam, you may be presented with this algorithm in a different form,
and you could be asked to present your answer in pseudocode
or your preference of either the OCR exam reference language or a high-level program code of your choice.
Here's another example of a flow diagram.
Now, this algorithm calculates how much carbon to dose into a fish tank to keep nitrates at bay.
It starts by asking what the nitrate level is
and says if the nitrate is above 10, then output 3ml.
If not, is nitrates above 2.5, and if they are, output 2ml.
And if they're not, is the nitrates above 1, and if it is, dose 1ml.
And if it's not, dose half a ml before coming to a stop.
We can, of course, express this algorithm in pseudocode.
Here's a comment just to explain the purpose of the program.
Next, we've got a variable called nitrate, which is entered from the keyboard.
We ask if nitrate is greater than 10, then dose 3ml.
Otherwise, if nitrate is greater than 2 and a half, then dose 2ml.
Otherwise, if nitrates is greater than 1, dose 1ml.
Otherwise, dose half a ml.
It's exactly the same program, just expressed in a different way.
So, all flow diagrams use a common set of symbols.
There are many symbols, but six of the most popular ones that you'll be expected to know for the exams are shown here.
You've got a terminal symbol, and that shape represents the start or end of a process.
Then you have a process symbol, and this shape represents something being initialised, processed or calculated.
You have a decision symbol, and this shape represents the decision.
It will have a yes or no, true or false outcomes,
which will be represented by two separate lines or paths exiting the shape.
Then you've got an input output shape, and this represents the input and output of data to the algorithm.
And you have a shape representing a call to a subroutine.
A subroutine will have its own flowchart, but when you call it from the main program,
you need to express that call using this symbol.
Finally, you have a line, and the purpose of the line with an arrow
represents the control passing between connected shape.
It's the flow of the algorithm.
With pseudocode, there's no established convention of exactly what terms you should use,
but in your exam, the examiners will use a particular type of officially defined syntax so there is some commonality.
This formally defined language by the exam board is known as the OCR exam reference language.
As you will see all code in your exams presented in this way, it will be handy if you would become familiar with it.
You'll notice that it's not a particular programming language.
However, it does look a little bit like Python.
This is deliberate, as OCR are well aware that most schools are using Python to teach programming at GCSE.
However, don't panic if you're learning another language.
OCR have stated that if an exam question asks you to write syntactically correct code in the answer to a question,
then you may write your answer in either the OCR exam reference language
or a high-level programming language of your choice.
So, comments are denoted with a double slash,
inputs use the input command, and variables can either be numbers or strings, which are identified in double quotes.
The global command identifies a variable can be used in any subroutine or function
instead of it being declared inside the subroutine or function,
which would make it only available to that particular subroutine or function.
The keyword const defines a constant.
These are values that don't change throughout the life of the program.
Python doesn't actually support constants.
You'd have to initialise a variable and then never change its value while the program is running
to get the same kind of functionality.
Casting is about turning one data type into another, so STR is used to convert a number to a string,
int is used to convert a string to a whole number,
and float is used to convert a string into a number with decimal places.
The reason that you have to cast variables is because, in memory, they were stored with different bit patterns.
You output to the screen using the print command,
and iterations can be FOR loops, WHILE loops and DO loops.
Notice the syntax for each of these loops.
In Python, the control of the loop is determined by indentation,
but in pseudocode and indeed in the OCR exam reference language, it's quite common to identify where the end of the loop is.
Now, Python doesn't actually support DO UNTIL loops,
but you still need to be aware of them for the purpose of your exams.
The difference between a WHILE loop and a DO loop is that a WHILE loop will not necessarily have its code executed
but the condition is checked at the beginning of the statement.
Whereas with a DO loop, the code inside the loop will execute at least once
because the condition is checked at the end of the statement.
There are logical operators that you can use in condition statements, and these are AND, OR or NOT.
There are comparison operators.
Note that the double equals is equal to, and not equal to is an exclamation mark and an equals sign.
Different languages use different symbols to represent not equals to.
You've got a less than, a less than or equals to, a greater than, and a greater than or equals to,
and you must be careful not to confuse the different symbols.
There are arithmetic operators, addition and subtraction, signified with plus and minus that you'll be familiar with already.
Multiplication signified with an asterisk or star symbol.
A slash is a division.
MOD is modulus - this tells you if there's a remainder when you divide one number by another.
So example, 6 divided by 2 is 3, there's no remainder, so the modulus of 6 MOD 2 would be 0.
However, 7 MOD 2 or 7 divided by 2 would be 3.5 and, therefore, the output would be 1, there's 1 remainder.
DIV is integer division, so when you divide one number by another, 7 DIV 2, the answer you'd get would be 3.
It disregards any decimal places that are the result of the division.
And then finally, you've got exponentiation or to the power of.
Selection is performed with IF, THEN, ELSE, ELSEIF and ENDIF.
And again, in Python, the condition is controlled by indentation,
but it's not uncommon in pseudocode and other languages to have an ENDIF statement.
An alternative to multiple ELSEIFs is to use a CASE SELECT or SWITCH command,
where you say SWITCH X and then have various case statements which can match.
With this selection statement, you can have more than two outcomes, and the default is the equivalent of an ELSE.
You terminate the SWITCH command with an ENDSWITCH.
Again, Python doesn't support the SWITCH command.
Instead, you have to use a series of ELSEIFs.
And then you have the string handling commands.
Dot length returns the length of a string.
Dot substring extracts characters from a string.
Dot upper converts the string to uppercase and lower to lowercase.
ASC returns the number of a character, and CHR returns the character from its ASCII number.
Then you've got subroutines, which are split into procedures, which are defined using the keyword procedure,
or functions, which are defined using the keyword function.
Both may take a number of parameters or variables that they can use,
and with functions only, if you wanted them to return a value, you'd use the keyword return.
You'd terminate subroutines with either endprocedure or endfunction as appropriate.
Arrays in exams will be declared using the array command, and again, notice in Python, we don't have arrays, we have lists,
but to all intents and purposes, we can think of them as really the same thing.
You also need to be aware of 2D arrays so it's possible to define a grid structure using two dimensions.
You need to be aware of how to generate random numbers.
These are generated using the keyword random followed by two parameters, which are the range of numbers,
e.g., random(1,100) would generate a single random number from 1 to 100 inclusive.
Finally, you look at file handling.
So, you can use open to open a file for read or write and close to close a file once you're done using it.
We use the endOfFile function to say whether we've reached the end of a file,
and we use dot readLine or dot writeLine to read a line or write a line to the file we currently have open.
And these are all the commands that you'll be expected to know in the exam.
Now, this video has assumed that you've got plenty of programming experience already
and therefore, you recognise a number of these commands.
If not, don't panic.
As you get more practice with programming, a lot of these commands will make sense,
but it's helpful to know how examiners will use syntax in an exam.
(uplifting piano jingle)