Learners will be able to:
- What Looping is
- Define what an iteration is
- Create a range of Loops and evaluate their use
- Launch a Rocket in three lines of code!
In this lesson you will learn how to loop a program. Loops are important if you want to do the same thing a number of times but use the same code. For example, add two numbers together and then add another two numbers together and then add another....
You will look at a number of ways to create a loop and then consider which is most suitable and why. Use your new skills to solve some problems. |
Summer Homework
- Get a head start with coding, sign up for this online course and complete as many of the activities as you can. The student with the highest percentage wins a prize.
- Like games? like coding? Learn to code by playing a game
Part 1: Launch the Rocket
- You are going to launch a Rocket, it requires a countdown from 10 to 0, then you must use the command "We have lift off"
- Run the program below, it uses the code print ("10") to display the number 10
- Create a program which counts down from 10 to 0 and then prints "We have lift off"
Part 2: Too Much Code!
- Think about the issues with this program? What are they?
- A more efficient method would be to loop and use the same code ten times, but how does the program now when to stop?
- You use a variable to store the 'number of times' the loop has run, this is called an iteration, more on the later.
- Then use a while loop, while something meets a condition then do an action
- Try the example below, can you create a countdown?
Part 3: Iteration
- Good work, you just iterated! An iteration is: the act of repeating a process with the aim of approaching a desired goal, target or result. Each repetition of the process is also called an "iteration", and the results of one iteration are used as the starting point for the next iteration.
- Why is this method better?
- How many lines of code do you have?
- What are the problems?
Part 4: Using a "for" loop, my name is Jeff
- The other loop that you can use if called a for loop, if you had a list of three numbers, for each of the numbers it would do something. The loop runs until it has down the action for all the things in the list.
- Add a print statement on line 6 to print the number 5
- Can you use this method to make a countdown?
Part 4: Making the Code Even Shorter
- It is possible to make the code even shorter using the range(0,0), this creates a list of numbers between two value, for example range(20, 25) would create numbers, 20, 21, 22, 23, 24
- Work out how many numbers the code below will display
- Use the print command to print out the countdown (hint: print i)
- Can you find a way to reverse the range?
Now try your skills with these challenges
Challenge: Noob
Print out the numbers between 1 - 20, each time it should print, "the number is 1", "the number is 2" and so on |
Challenge Wizard:
Create a variable called count with a starting value of 0. While this variable is less than 100, print the phrase, "Turned on the computer, Adobe doesn't need an update". Add 5 to the variable each iteration. |
Challenge E1337:
For the numbers 1 - 20, times the number before by the next number, for example, 1 x 2, 2 x 3, 3, x 4 and then print out the answers, 2, 6, 12 and so on |