-[Craig] In this video, we're going to go over the use of the three basic programming constructs.
That's selection, sequence and iteration.
(uplifting piano jingle)
In order to explain these programming constructs,
we're going to use the example of a simple program for a game called Beat That Dice,
which we went over in the previous video.
So the first of the three programming constructs
and the most simple and straightforward is sequence.
And sequence literally means executing one instruction after another,
and by default, if your program isn't doing selection or iteration or anything else special,
it is always working in sequence, that's simply executing one line
and then moving on and executing the next line and then the next and so on.
If programs could only operate using sequence, though, they wouldn't be very intelligent.
Branching is a construct that allows the program to end up going in a number of various directions
depending on the outcome of a condition.
So the first typical selection construct you learn about is typically the if statement.
So here we can see if dice one is greater than dice two
then we execute the first line of code.
Otherwise, or we say else, execute this line of code,
and this is quite typical and something by now you'll probably be quite used to.
So here we can see at the bottom,
if user input equals roll value, print you worked it out correctly.
Otherwise, print no, the value of the roll is something else.
So this is really common and is the first typical selection or branching statement you learn.
The final programming construct is iteration, often known as looping,
and this is where we get to repeat sections of code.
There are several ways of doing this.
A popular one is the for loop.
And a for loop is known as a counter-controlled loop,
and we use this when the number of iterations needed is known ahead of the iteration executing.
So for example here, we're saying for rolls in range rolls per player,
and if rolls per player was nine, it would be for rolls in range nine.
So we know the number of times we want to execute or iterate, repeat, this code.
You also need to be aware of a second type of iterational looping construct
known as a condition-controlled loop.
And in Python, this is supported with while loops.
While loops are used when the number of iterations is not known
because the variable used to determine when the iteration ends
is changing within the iteration itself.
So previously, for example, with a for loop,
we would say for 1 to 10, a fixed number of times.
With a while loop, as an example here,
we've got a condition,
while answer is not equal to computer,
and there's a single line of code that says answer equals input, what is the password?
And this line of code would keep being displayed to the user,
we would keep repeating it in the while loop,
asking them again and again to enter their password until they entered the string computer.
So this is a condition-controlled loop that only exits when the condition is matched.
It's also perfectly acceptable for you to nest programming constructs inside one another.
And you can achieve this with both iteration and selection, and indeed a mix of both.
So here on the left, we see a for loop nested inside a while loop.
And of course, you can easily spot the nesting in Python because we use the indentation.
So here we say while solved is not equal to true, run an instruction
and then enter an inner nested for loop for one to five times.
Those instructions we run five times.
We then run one more instruction in the outer while loop and then continue back round again.
Likewise on the right, we have an if statement nested inside an if statement.
So we say if the game one variable equals true,
then run an instruction and then run another if statement nested inside.
And if score is greater than high score, then we execute the line of code underneath.
We then break out to the outer if statement and run one more line of instruction code.
So that's the theory content of the video covered.
We're just going to spend a little bit of time talking about the programming language subset.
If you've heard this before,
you can skip the rest of this video.
Otherwise, it's definitely worth a quick watch.
So the exam board Edexcel have released a document that they call the programming language subset,
which you can see here on the right.
This document specifies which parts of Python 3 are required
in order that the assessment can be undertaken with confidence.
Students familiar with everything in this document
will be able to access all parts of the paper two on-screen assessment.
The exam board will not go beyond the scope of this document
when setting you any questions in the paper two exam.
Any students who do successfully use more complex methods which are not included in the document
will still, however, be awarded marks.
Further to this, you will see here from the front of the exam paper
that you are actually going to be supplied with a printed and an electronic copy of the PLS
during your paper two exam.
Well if this document outlines everything you need to know
in order to program in Python during the exam,
and you can be guaranteed that you can answer all the questions
using all the techniques in this document for full marks,
then you might as well get used to using it now.
Ask for a copy from your teacher,
and have it as a reference guide at all times throughout your GCSE course.
It will be invaluable.
(uplifting piano jingle)