Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE

## What am I Learning today?

 I am learning how to use simple loops in a program to repeat specific behaviour I am learning how to use determinite loops to repeat code a specific number of times I use a for loop to go through the characters in a string

## Lesson

#### Don't forget you have until the end of term to create your text adventure game

1. Set your Learning Objectives to red.

2. We are going to save all of our work for this topic into this PowerPoint.

3. #### To download the PowerPoint by clicking on the image below

4. Save your PowerPoint as 'Programming 1'

## Coding loops

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 button in the top right hand corner of the page.

The code should look like this:

## Writing a simple For Loop

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.

• Here is the code for you:
• for i in range(5):
print("hello")
1. Copy the code above into your python window.

2. Save the file name as for_loop.

3. Run the code.

You should see this

### Activity 1

1. Change Line 2 to print out something instead of 'hello'.
2. Change the number in the loop to print out a different number of times

### Extension Activity 1

1. Print out the value of i in front of the text.
2. Ask the user to input a number and use that value in the loop to print out a different number of times.

• When you are finished don't forget to save your powerpoint

• 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.

### Activity 2

1. Change the loop so that it prints out the number range 3 to 10.

### Extension Activity 2

1. Ask the user to input a number and use that value in the loop to print out a different range of numbers.

• When you are finished don't forget to save your your powerpoint

• Below you can see I have answered one of the extension questions using Edublocks.

### Activity 3

1. Have a play yourself see what you can do.

• When you are finished don't forget to save your your powerpoint

• ### Summing it all up

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

## Learning Outcomes I need to learn how to use simple loops in a program to repeat specific behaviour

• I have a basic understanding of how I can use simple loops in a program to repeat specific behaviour with a little help from my teacher
• I can show my teacher that I can use simple loops in a program to repeat specific behaviour without their help.
• I can use simple loops in a program to repeat specific behaviour independently and I can also explain it to others and can complete any extension tasks I am given.