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 am also pleased that you have decided to learn about nested loops.

We are going to do brilliantly.

I'm so excited for this lesson.

Let's have a look at the outcome.

So the outcome is: "I can create programmes that use nested loops to solve problems." Great! Let's crack on with it.

We've got three keywords in today's lesson.

We've got nested loop.

So this is the process of placing one loop inside another loop.

We've got outer loop.

This is the loop that contains another loop inside it.

And then we've got the inner loop.

This is the loop that is placed inside another loop and runs completely each time the outer loop runs once.

So that could be a bit confusing.

Perhaps you want to pause the video and reread the definitions.

But we've got nested loop, outer loop, and inner loop.

So this lesson is split into two.

The first part is to explain how nested iteration repeats code.

And the second part is modify code to include nested iteration.

I'm so excited about this lesson.

Can't wait to start.

So let's take a look at the first part of the lesson, which is explain how nested iteration repeats code.

Nested loops involve placing one loop inside another.

The outer loop controls how many times the inner loop is repeated overall.

The inner loop controls how many times its own block of code runs each time it is triggered.

Each time the outer loop runs once, the inner loop runs from the beginning.

This may seem really complicated, but throughout this lesson we are going to learn more and more about this.

So well done and we can carry on.

A nested loop is when one loop is placed inside another.

So you can see here on line 2 we've got another loop, which is a nested loop because it is placed in the loop that starts on line 1.

A nested loop is when one loop is placed inside another.

If you pass one value, then the range () function will use it as the stop value and the sequence will start at 0.

So on line 1, we've got, we've passed the value 2 into the range () function.

So that'll be used as the stop value.

So this will generate a sequence of numbers.

So on line 2, we've got the start number now, which is 10, and we've got the stop value, which is 13.

So this will generate a sequence of numbers.

10, 11, 12.

When the programme is executed, the outer loop runs for the first time.

The inner loop runs three times.

So you can see the output on the right hand side of the slide.

So our first outer loop is 0 and then it's 10 followed by 11, followed by 12.

Okay, so you can see that inner loop is printing 10, 11, 12, but the outer loop is printing 0, which is the start of its range on line 1.

So when the outer loop runs for the second time, the inner loop still runs three times, but now the outer loop is now 1.

So it's 1 10, 1 11, 1 12.

So the outer loop runs for the first time.

We can see that the inner loop now has a value of 10 and the outer loop has a value of 0.

So this is output by 0 10.

And then on the table we can trace through and go, the outer loop is 0, the inner loop is 11, so the output is 0 11.

Then we can loop back and say the outer loop is still 0, the inner loop is 12, so therefore the output is 0 12.

Then we go to the outer loop because we finished all the loops in the inner loop, and we can say now the outer loop is 1, the inner loop has gone back to 10 'cause it started at the start of the range again and the output is 1 10.

We loop around again.

So the outer loop is still 1, but the inner loop is now 11.

So we output 1 11 and we loop around again on the inner loop.

So we have the outer loop still 1, the inner loop 12, and then the output is 1 12.

So hopefully you've understood that, but don't worry.

Remember you can pause or rewind the video at any time and go through it step by step.

Let's have a quick check.

How many times will this print a line? Is it A three, B: six, or C: one? So familiarise yourself with the code.

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

So let's have a look at the answer.

It is B: six, because the outer loop is going to repeat three times and the inner loop is going to repeat twice.

So 3 times 2 is 6.

It is not just for loops that can be nested inside each other.

This programme uses an inner while loop inside an outer while loop.

So you can see the code and you can see the text output.

So take your time, look at the code.

Can you work out the while loops going on there? So the out, the line variable is 1 on line 1, and then on line 2 we've got while line is less than or equals to 2, do this.

So repeat equals 1.

Okay, so while, then our inner loop is while repeat is equals or less than 3, print line and then the value held in the line variable, and then repeat plus equals 1.

So add 1 onto repeat.

Then line add 1 on equals 1.

So add 1 onto line.

So can you see how that matches up the output on the right hand side? We've got line 1 printed three times, because line is 1 and then a inner loop repeats three times and then we've got, on line 7 then, we go to line add 1 to line.

So therefore line becomes 2, but then the inner loop is then repeated three times.

So you've got line 2 printed three times.

Hopefully you've worked that out.

Well done.

If not, you can pause the video or just rewind the section.

The outer while loop runs twice.

Once for line equals 1 and then line 4 equals 2.

The inner while loop prints the message three times for each line.

So hopefully you understood that.

Remember you can pause the video and see if you can take your time matching the output to each line.

This programme uses an inner while loop inside an outer for loop.

So take your time and look at the code.

The outer for loop runs twice.

Inside it, a while loop runs twice, printing a message each time.

Can you see how that matches up with the output with the code? Let's have a quick check for understanding.

It's a true or false one.

Only for loops can be nested inside other loops.

Is that true or false? Remember, you can take your time and pause the video at any time or rewind it.

The answer is false.

Why? You can also nest while loops inside for loops, for loops inside while loops, and even while loops inside other while loops.

Any type of loop can be nested within each other.

So let's have a quick practise.

One: Explain how nested iteration is used in this code to repeat the print statement.

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

So an answer to explain how nested iteration is used in this code to repeat the print statement.

So one loop is placed inside another.

In this code, the outer loop repeats for each day, and for each of those days, the inner loop repeats through all the time slots.

That means the print statement runs lots of times, once for every combination of day and slot.

Well done if you got that correct.

Hopefully you can explain that outer loops and inner loops and how they work together.

Well done.

Well, we're going through this lesson really well.

Hopefully you're understanding most of it, if not all of it.

We are going to move on to the second part of the lesson.

This is now where we modify code to include nested iteration.

A programmer might use nested loops in real-life programmes when developing things like timetabling systems to loop through each day, outer loop, and each lesson slot, which would be the inner loop.

Board games or video games to loop through grid squares to place items or check moves.

So for example, like chess or connect four.

Or spreadsheet tools can be developed to loop through each row and column to process data in the cells.

So some real life programmes that can be developed.

Lists may be used when developing code in loops.

So with a list we have an identifier, which is days, and then we've got the list items. So in this case it's the days of the week.

So in this example, the list items are strings or literals, pieces of text.

So they need to be in quotation marks and separated by a comma.

Also note the list is going to go over more than one line due to the length of the items. When the programme is run, this is what the list will look like when stored in memory.

The first item in the list is at position 0, remember, not 1.

Let's have a quick check.

The first item in a list is located at position 1.

So is this true or false? Remember, you can rewind the video at any time or pause it.

Let's have a look at the answer.

It's false, but why is it false? Well, in most programming languages, including Python list indexing starts at 0.

So the first item is at position 0 and the second position at 1 and so on.

Well done if you got that correct.

This programme generates a timetable for each day, showing the lesson slots for each day of the week.

So there we've got our identifiers, days of the week, and lesson slots on lines one and three.

The days of the week list holds the days from Monday to Friday, representing the days in a weekly timetable.

So they don't have Saturday and Sunday in.

The lesson slot list represents five different lesson slots for each day, from 9:00am to 3:00pm.

Now let's look at the outer for loop 'cause this iterates over the days of the week list for each day, Monday to Friday.

The programme prints the timetable then for that day.

For each day, the inner for loop iterates through all the lesson slots.

It prints out each lesson slot under the current day.

Open this programme by following this link, oak.

link/nested-for-loops, all hyphenated, and run the programme to see the output.

You can nest additional loops within the inner loop.

So that's lines 10 and 11.

You can see that we've got another loop which is inside an inner loop, which is on line 9, which is inside an outer loop, which starts on line 7.

Take your time and look at the code.

A list of rooms in the school, Room 101, Room 102, Room 103, has been added on line 5.

For each lesson slot, that innermost loop iterates over the rooms, Room 101, Room 102 and Room 103.

Open this programme by following this link, oak.

link/nested-for-loop, all with hyphens and run the programme then to see the output.

Let's have a quick check.

How does the number of repetitions of the inner loop relate to the number of repetitions of the outer loop? Is it A: the inner loop repeats a random number of times, B: the inner loop repeats once for every repetition of the outer loop, or is it C: the inner loop repeats fewer times than the outer loop? Remember, you can pause the video any time or even rewind it.

The answer is B, the inner loop repeats once for every repetition of the outer loop.

Well done if you got that right.

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

Hopefully you can see why that is the answer.

Let's have a quick practise, then.

So Laura has four subjects to revise and you need to create a structured timetable to make sure she can fit in time for each subject across several days.

Okay, so take a look at this code here.

We've got two lists and a outer and an inner for loop going on.

This programme helps you create a revision timetable by organising study sessions into time slots for each day of the week.

So one, open the starter programme at oak.

link/nested-loops with hyphen in it.

And two, update the code so that for each time slot on each day, it also prints out each subject Laura will revise during that slot.

She's revising for four subjects: maths, english, biology, and history.

Remember to take your time, pause the video if you want, and we may have done something very similar to this previously, so you can rewind the video and take another look at that.

So a possible answer here.

So we've added in a few lines and another loop within the inner loop.

So on line 4, we've got the identifier, subjects and we've put the subjects in as a list.

So we've got Maths, English, Biology, and History.

And then to iterate over that list on lines 10 and 11, we've put, for subject in subjects: print the subject.

Okay, so it prints each of the subjects within the inner loop that was there, which is on line 8.

So that was for slot in timetables, slot.

Sorry, for slot in timeslots, which is in our outer loop, which is on line 6, which started for day in days of the week.

Okay, so hopefully you got something very similar to this and if you didn't, hopefully you can recognise the correct answer.

Well done.

Brilliant.

We've come to the end of the lesson.

Really well done getting through this one.

It was very tough with learning all about different types of loops.

So in summary, nested loops involve placing one loop inside another to allow repetition at multiple levels.

The outer loop controls how many times the inner loop repeats, setting the structure for how many overall cycles occur.

Each time the outer loop runs once, the inner loop starts again from the beginning, repeating its code for each outer cycle.

Really well done.

It's an amazing lesson.

I've really enjoyed helping you through it.

And remember, you can always pause and rewind the video at any time to go over anything that you want to look at again.

Well done.