Introducing loops - For
Introducing loops - While
Set your Learning Objectives to red.
We are going to save all of our work for this topic into this PowerPoint.
Save your PowerPoint as 'Programming 1'
In computer science, a loop is a programming structure that repeats a sequence of instructions until a condition is met. Today we are going to look at the For loop.
But first...
Click on the link above to open up edublocks
See if you can make the code using the blocks below
Now copy it into into either Codeskulptor or Thonny by clicking on the Blocks button in the top right hand corner of the page.
The code should look like this:
When coding we sometimes want to repeat lines of code. We could do this by writing them out again, but it is much better to use a loop to repeat them.
A for loop is a counting loop. We tell it how many tims we want it to repeat, then it counts up to that number and then stops.
We are now going to create a simple for loop that repeats 5 timesand prints out 'hello' each time it repeats.
We are now going to create a simple for loop that repeats 5 times and prints out 'hello' each time it repeats.
Copy the code above into your python window.
Save the file name as for_loop.
Run the code.
You should see this
In your Powerpoint put a screenshot of your code.
We are now going to look at how the for loop works. Remember it is a counting loop that runs a specific number of times.
In python we can use the range() function to tell our loop how many times it is going to run. So if we wanted it to run 3 times we would use range(3).
We do have to keep a count of how many times the for loop has already run, so for that we use a counter or iterator, which keeps a track of how many times the loop has already run and checks against the range value to see if it is time to stop.
Our counting variable can be called anything we want, I have called mine i in the code above which is shore for iterator but you could call it whatever you want.
Look at the code below and copy it into your python program and run it.
Below you can see I have answered one of the extension questions using Edublocks.
In your Powerpoint put a screenshot of your Edublocks code.
Lets look at the learning outcomes and decide which one best describes our current level of understanding :
Tick the one you feel is closest to your level