Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.

Similar presentations


Presentation on theme: "Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input."— Presentation transcript:

1 Learning to use a ‘For Loop’ and a ‘Variable’

2 Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input so that the user can pick how many times they would like to repeat

3 During the last program you made had a lot of lines to write. Can you remember the problem with this? What was the problem?

4 Getting Started 1.You will need to load up your Minecraft world from the previous lesson. 2.Make sure that you’re able to edit the code you created. It should have been called something like ‘myfirstmovement’ 3.Load up the edit screen!

5 ‘For’…. loop A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.

6 How it works… Your ‘for loop’ is built up from different sections. You have the variable which is your start number. It is then separated by a comma and your finish number You then finish off with how many steps you will take. This is where the code you wish to repeat appears!

7 What is a variable… A variable is a location in memory in which you can temporarily store text or numbers It is used like an empty box or the Memory function on a calculator You can choose a name for the box (the “variable name”) and change its contents in your program Some people refer to a variable as a bucket…

8 Naming our variables A variable name can contain only numbers, letters and underscores A variable name cannot start with a number You can’t use “reserved word” as a variable name – for example class is a reserved word so class = input() will result in a syntax error

9 Give it a try! This example shows you how to create a straight line of which ever block you have decided to use

10 Challenge! Use a ‘FOR LOOP’ to build a structure! You will need to try a combination of loops to build different shapes. See if you can build at least 3 rows of blocks. Extension: - Can you put loops with in a loop to build a whole house?

11 How can I chose the length of my line? Sometimes it’s useful to ask the user to interact with the program. We can save their input into a variable. The lines above will store the length into a variable called LENGTH – What do you think we will do with length?

12 How can I chose the length of my line? If you have a look at the code above, can you spot where we have used the variable we called on? *HINT IT’S NO LONGER NUMBER 4*

13 Challenge? Can you create a program which asks for a number Of different variables. This could be height and Width and you can see if you can create a wall for a building….. Requirement: - Use a for loop along with a variable for the length / height

14 While Loops Sometimes we do not want our program to keep building, we might want to check if we still have the resources to do it. What if we didn’t have enough wood left? We might want to keep building until we run out. This is why we would need to check a condition……. Think about you being in a lesson, you’re working on IT/Computing until the bell goes for the next lesson. We can even use a while loop to check which number we’ve got to. An example is on the next slide.

15 While Loop This line converts it to a number This lines compares it to see if it’s still less This increases the total by one before it’s checked at the beginning of the while loop

16 Challenge? Are you able to modify your existing program to make use of the while loop within your program? You should use variables and ask for input before it works.

17 Finishing Up! Now that you’ve learnt how to use a number of different loops we’re going to print off the code we’ve created and we’re going to comment our code. Commenting code is really important and shows your understanding. To comment your code we’re going to use - - ‘two hypthens’


Download ppt "Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input."

Similar presentations


Ads by Google