Lesson video

In progress...

Loading...

Hello, my name is Mr. Hogan.

I'm excited to be learning with you today.

We are going to have such a great time learning about programming iteration.

I will be supporting you with our learning during these lessons.

I'm so pleased you have decided to learn about using trace tables.

We are going to do brilliantly.

Let's have a look at the outcome of the lesson.

It is, I can use iteration controlled by account value to repeat sequences of code.

We have two keywords this lesson, iteration, so this is the process of repeating a sequence of instructions within a programme loop.

Then we have for loops.

So this is an iterative statement that we'll repeat for the length of a given sequence.

So these are the key words we are going to be using throughout this lesson.

When we come to them, they'll be emboldened and if you want to come back to this slide to remind yourself of what they mean, feel free to do so.

So I'm really looking forward to this lesson and it splits into two.

Our first part is to explain how a for loop repeats code.

And then the second part of the lesson is modify code to iterate using a for loop.

So fantastic, we're learning all about for loops today.

Let's move on to the first part, which is explain how a for loop repeats code.

This is an example of a for loop.

A for loop is a tool to control the flow of execution in your programmes.

When you use a for loop, you are saying for every element in this sequence, do this.

So it could be that like this is, for this sequence on line one, do this, which is the code the line after.

For loops can be used for many types of sequence.

For example, letters in a word, items in a list, numbers in a range.

Let's have a quick check.

It's a true or false one.

A for loop is a tool to control the flow of execution in your programmes by repeating a set of instructions.

Is that true or false? Remember, you can rewind the video anytime or pause the video.

The answer is true.

Why? While for loops are a control flow mechanism used to repeat a block of code for each item in a sequence.

Well done if you got that right.

This lesson will focus on for loops that use the range function.

Range is the sequence that you are going to iterate through.

So here we can see line one of the code, in range five.

Range is a built-in function.

Just like input, it generates a sequence of numbers.

When you call the range function, you can pass it up to three values.

So on line one we've got the values of 0, 10, and 2, being passed into the range function.

The first value is a start number.

So in this case it's zero.

Then we've got an end number.

So in this case it's 10 and we have a step number, and in this case it's two.

So it steps up every two.

If you only pass one value, like in this example we've passed in five, then the function will use it as the last number.

When you call the range function, it will generate a sequence of numbers 0, 1, 2, 3, 4.

The end number is not included in the generated sequence because it is used as the stop point, so not included.

Count is the variable that is used to iterate through this sequence.

So on line one we've got count which is highlighted, which is the variable that is used to iterate through this sequence.

Let's have a quick check.

What is the primary purpose of the range function in a for loop? Is it A, to define the code that will be repeated? Is it B, to specify the variable that will change during each loop? Or is it C, to define the sequence of numbers that the loop will iterate through? Remember, you can pause the video at any time or rewind it if it helps.

So the answer is C, to define the sequence of numbers that the loop will iterate through.

Well done for getting that right.

Excellent.

And if you didn't, don't worry.

Hopefully you can understand why that is the answer.

Let's have another check.

What will be the last number printed by the for loop in this code? So I'll take a look at that code.

Is it gonna be A, 10, B, 12 or C, 14? Remember you can rewind the video to help you or pause it to have a little think about the answer.

So the answer is 12.

Why? Well, the loop runs through the values of count in the range from zero to 14 with a step of two.

So the values printed will be 0, 2, 4, 6, 8, 10, and 12.

The end number is 14, is not included in the generated sequence because it's used as the stop point.

So remember that 14 is the stop point.

It is not included in the print.

Okay, let's have a look at a few more.

So we've got process, we've got a function, we've got state, and we've got the output on this slide.

So the process is the range function will generate a sequence of numbers.

So the function we're using is range three, so 0, 1, 2.

And the state of the count variable is at the moment blank.

We can move on because now we've said that count equals zero and therefore on line two the output will be zero.

So zero will be displayed.

We then go to the start of the for loop again.

And we go count holds the next value in the sequence.

And therefore it goes to one.

The value held in count is displayed as the output.

So it's now one.

Go back to the start of the for loop count holds the next value in the sequence.

So therefore that's two.

And then on line two again the value held in count is displayed as the output on screen.

The end of the sequence is reached so the loop terminates.

Remember three is used as the stop point.

Pass one value and it'll be used as the end point remember.

Pass two values and they'll be used as the start and end point.

Pass three values and they'll be used as the start and an increment or step value.

Let's have a practise.

One, explain how a for loop repeats code and provide an example of a for loop that prints the first five even numbers.

Your output should look like this.

So it should be an output of 2, 4, 6, 8, and 10.

Remember, you can rewind the video to look at previous slides.

We've done something similar to this.

And pause the video if you want more time.

So let's have a look at an answer.

A for loop repeats a block of code a specific number of times based on a given range.

In the loop a variable takes on values from the specified range.

And the block of code inside the loop runs once for each value.

So the example of the for loop is range 2, 11, 2.

Creates a sequence of numbers from two to 10, stepping by two each time.

So the output would be 2, 4, 6, 8, 10.

The before loop then prints each number in the sequence repeating the print statement for each number.

Well done for getting this right.

Hopefully you've got most of those points, even if you've got some, that's brilliant.

Well done.

I'm really pleased how this lesson is going.

Right, it's time to move on to the second part of the lesson now.

So this is where we're going to modify code to iterate using a for loop.

Well done on getting this far.

That's amazing.

Well done.

This is the first time you've used for loops, so keep going.

So this code works, but it's not very efficient, is it? It's five lines and we're printing values from zero to four.

So if you wanted to create a programme to output zero to four, you would have to write five print statements if we followed this approach.

Sofia has written a simple Python programme, that asks the user to input their name three times one after the other.

So take a look at that code.

You can see that lines one, two is basically repeated on lines four, five, and seven and eight.

This only works for three users and it's repetitive.

Sofia would have to copy and paste more lines of code for more users of the system.

What if the programme asks how many users there are? You can add a variable to store how many users you want to welcome.

Then you can use a for loop to repeat the same action that number of times.

A good question to ask Sophia.

Let's have a quick check.

It's a true or false one.

A for loop can be used to repeat the same action multiple times, is it true or false? Remember, you can pause the video at any time or rewind it to help you.

Let's have a look at the answer.

It is true.

Why? Well, for loops are designed for repetition, executing a block of code a set number of times, or for each item in a sequence.

Well done.

Let's have a look at this code.

This line asks how many users there are, and stores that number in a variable called num_users.

This line uses a for loop to repeat the next lines of code that many times.

Then these lines run again and again for each user.

Let's have a quick check.

In this code, what determines how many times the for loop will be repeated? So take a look at that code.

Is it A, the value stored in the name variable.

Is it B, the value stored in the num_users variable.

Or is it C, the range function always repeats five times.

Remember, you can pause the video at any time or rewind it.

Let's have a look at the answer.

It is B, the value stored in the num_users variable.

Yep, so that determines how many times the four leap will repeat.

Well done if you got that right and if you didn't, don't worry, you're doing really well.

So let's have a practise.

Question number one.

We've got some code.

So amend the programme so the user can specify which times table they want to display.

The programme should then display the correct times table based on the number the user enters.

Okay, so we've got some code there.

Take your time to look through it.

Remember you can rewind the video at any time and pause it.

Good luck.

So let's have a look at the answer.

So this is how we've amended the programme.

We've just put some code in line one.

So we've removed the five that was there and we've put input an integer after being prompted, enter the number for the times table.

So now the user actually enters the number that they want.

Seems very simple, doesn't it? But hopefully you understand why that is the case.

Excellent.

Well done.

Two, this is where we open a starter programme at oak.

link/for-loop-times-table, all hyphenated.

So once you open that, you can do question number three.

So change the values in the range function so that the programme outputs the timetables from one times five up to 12 times five.

Remember, you can pause this at any time or rewind it.

So a possible answer, hopefully you looked at line four and we looked at the range one to 13.

Remembering that 13 is the stop point.

It is not displayed, not used in the range.

Well done, you've come to the end of the lesson.

That's really good.

So in summary, iteration can be controlled by a sequence or of count value, which determines how many times instructions are repeated.

In Python for loops are used to implement count controlled iteration, allowing precise control over the number of repetitions.

And that's really good.

Well done for coming to the end of the lesson, and if there's parts of the lesson you don't understand still, or you want to look further into things, then please rewind it and remember you can pause the video at any time, but well done for getting this far.