Lesson 3 FOR loops

FOR loops and The Clock Project

What are For loops?

We have seen a FOR block before

We used a FOR loop in the last project, we did in the last lesson walkaround1. The cool kid moving along the path. Without the loop we would have to work out how far we wanted to the cool kid to go, and we could only run it once. Remember the variables from the last lesson we are using it here, it has a number 1 in this bit of code. So it will mean in this case add 1 to the x position so the cool kid moves one step to the right on each FOR loop. 

1st Demo

So now we can put code together in sequences, and can work with the variables. This is great but you should be seeing us doing the same thing over and over again. If we look at the code in the first demo we looked at sequences. It would be better if it could go in a FOR loop as we can see in the above picture. Now you only have to write the sequence once when it is written in a loop.

You will understand this or you may need more help, 75% will need more help. Ada and Alan are always happy to explain. The point here is we are using a loop to read a word left to right. Think back to the variable lesson where we went through the letters in a numbered box. (We count through them in our 'thisnumber' variable.) can you see it getting 1 added to it every loop. Also when this count gets to the number of letters in the word we entered it will exit the loop. When you get this you are well on the way to becoming a coder. WELL DONE

The 1st Demo project

I need more help

Setting Everything up

Lets have a look at the setup part of this code. It is important to set everything up so when you run though the code everything is how you expect it. In this case we are setting all the strings to the value of '' (we call this empty string) and all the numbers to the value of 0. Please go back to lesson 2 if you are unsure what is and how we use variables. When we have done all this we use the light blue ask blocks to ask 'What's your name?' You can see in the picture below the question is shown in the cats speech bubble, then the user can type text into the enter text box at the bottom. The answer is held in the light blue answer holder, this is in fact a special variable that can only be used for holding the text you enter. When you look at the writting on the light blue ask and wait block, what that means is the code just waits for the user to click the tick on the text box. When the user has clicked the tick box the WhatwasSaid variable is give the value that is held in the answer holder. The last part of the set up is we set the thisnumber variable to the value 1. The reason we have this is to count the letters in the text entered.

The work is done in the FOR loop

The reason we use loops is to do the same thing many times. In this loop we will do the work until we have read all the letters of the text entered. So let assume we entered 'Jon' as in the example above. We are going to look at the words 'repeat until thisnumber > length of Whatwassaid', this is the way the computer can keep track of how many letters it has read.

As we saw in the set up we set the thisnumber variable to 1 and length of WhatwasSaid is a number that is the number of letters in the text entered.

So when we start thisnumber is 1 and the number of letters in whatwasSaid is 3  the > symbol means the value on the left is more than the value on the right that is in the first case.

  • 1 is not more then 3
  • 2 is not more that 3
  • 3 is not more than 3
  • 4 is more than 3 so we finish.

MyName is set to '' so we join the letter at poistion thisnumber

  • first time to equal 1
  • second time to equal 2
  • third time to equal 3 ect...

You can see the thisnumber getting 1 added to it at the end of each loop. This gives the the result you can see below where 'Jon' is written out 1 letter at a time.     

second demo a game to play

This the first time we have tried to have a complicated set of code but you have to have a few things going on to play a game. Think of a real tennis game you have to set the score to 0 0. Then you need a player that can run around, we are doing this by setting the bat to the mouse position. We have to keep checking where that mouse is, so every time the computer thinks, that is fast. We move to the mouses position in a loop once around every time it thinks. Imagine trying to do this in a sequence. The player has to serve the ball to start and hit it back, in this game it only goes in the opposite angle, no need to worry about that yet. We have to be ready for the ball all of the time so another loop. with an IF test that's our next lesson. We call a computer think time a clock tick.

We on Facebook

The pong game was the focus for a step by step daily guide on our Facebook site. It's there of you so please feel free to use it.

2nd Demo Pong Game

The Clock project

The Clock Project

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.

Can you make this clock tick. The time display will need to show the number the clock is pointing to. You have a clock sprite with the clock pointing at all the 12 positions. The clock will run at one tick per second from the moment the green flag is clicked until the red spot is clicked. If you need more help click on the button below.

I need more help

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.