Tutorial 7: For Loops
Hello again!
Okay, we know how loops work and we understand variables. Now we can cover another incredibly useful tool for programming: For loops.
For loops are used all the time in programming. Sometimes used for animations, sometimes just as a counter, sometimes used to check over a whole array very quickly, For loops are a valuable and versatile tool.
1. for i = 0 to 10 loop
2. print( i )
3. update()
3. repeat
4. sleep( 2 )
The program above is a counter from 0 to 10. For loops are special because they define a variable within the loop.
In our example, we are defining a variable called i
. The first time this loop goes around, i
is equal to 0. As we know, when FUZE reads the repeat
command, it returns to the last loop
line. However, now i
is equal to 1. Next time the loop goes around, i
is equal to 2. This carries on increasing by 1 each time until we finally get to 9.
When i
is equal to 10 the condition is completed and the loop finishes!
Note: The loop counts up to but not including the last value. So our example, for i = 0 to 10 loop
will count from 0 to 9 and will not reach 10.
Using a For Loop to Create Sound
So why might we want to do this? Well, below is quite a cool example of how to use a for loop to create interesting sounds using our playNote()
function. Change your code so it looks like the program below and run it. Make sure your volume is up!
1. for i = 0 to 900 loop
2. playNote( 0, 1, i, 1, 10, 0.5 )
3. update()
4. sleep( 0.01 )
5. repeat
Here, we have used our variable i
again, but this time we are counting to 900 instead of 10. Remember, we will never quite reach 900. i
will only ever reach 899.
The playNote()
function is quite a cool one to get used to because it can be used to create your own music! playNote()
plays a note of a certain frequency (pitch). In this case, our frequency is determined by the i
variable.
The first time around, our playNote()
will play a frequency of 0hz. The next time around the loop, the frequency will be 1hz, then 2hz and so on until 899hz. This will give us a sequence of notes in a loop.
We are also using a sleep()
function with a very short delay of 0.01 seconds. With a delay this short, the notes sound more like a sweep upwards, kind of like an alarm sound or a siren.
What if we wanted to play a more musical sounding series of notes? Well, we'll need two things: a longer delay, and something called a step.
We use a step in a for loop to count in specific amounts. Take a look below:
1. for i = 0 to 900 step 100 loop
2. playNote( 0, 1, i, 1, 10, 0.5 )
3. update()
4. sleep( 0.1 )
5. repeat
In this example, our for loop still counts from 0 to 900, but in jumps of 100 each time. This means the first cycle of the loop is still 0, but the next is 100, then 200, 300 and so on. However, this time when the i
variable reaches 800 the loop finishes.
We have used a longer delay this time to give us a longer note duration. The result will sound like a scale moving upwards. Much more musical!
Try using different steps and beginning or end values to get different results!
Using a For Loop with Shapes
Another example of how we can use a For Loop is for animation. Let's say we wanted to draw a line one pixel at a time across the screen.
1. for x = 0 to gwidth() loop
2. box( x, gheight() / 2, 1, 1, white, false )
3. update()
3. repeat
Notice in this example we are naming our variable x
. This is because we are using it to change the x axis position of a pixel. You can of course call your variables whatever you feel like!
Our x
variable will count from 0 to the maximum number of pixels along the x axis of the screen.
When we use the box()
function, we must put certain pieces of information in the brackets. The first two pieces are the x and y position of where we want our box to appear on screen. Next up, we have the width and height of the box. In our example, our box has a width and height of just one pixel. A very tiny box indeed! The next piece of information is the colour we want our box to be. Lastly, we say whether we want an outline of a box (true
), or a filled in box (false
).
For the x position, we are using our increasing number x
. For the y coordinate, we are using gheight() / 2
, which gives us the middle point on the y axis of the screen.
In short, this little program will draw a line across the middle of the screen, one pixel at a time. Watch it in all its glory!
Let's do something a bit more visually exciting than just a line.
Change your code to look like the program below, but see if you can figure out what will happen before you run the program!
1. for y = 0 to gheight() loop
2. circle( random( gwidth() ), y, 100, 32, fuzepink, true )
3. update()
4. repeat
gheight()
as we know is a function which gives us the height of the screen in pixels.
In handheld mode, that number will be 720. In TV mode, that number is 1080.
Depending on how you are using your Nintendo Switch console, you will see either 720 or 1080 circles appear on you screen, one by one, moving down the screen.
Since the x part of the circle()
function is a random number chosen out of the total number of pixels across the width of the screen, our circles will appear in a random position along the x axis each time a new one is created.
Using a For Loop with an Array
One of the most useful and practical applications of a for loop is using it to cycle through an array of information.
This technique is used a lot in programming. If we have lots of things to put on screen, chances are those things are stored in an array and a for loop is used to display them.
Let's see an example of using a for loop with an array to print lots of different names on the screen.
First, we'll need to create an array and populate it with information.
1. array names[5]
2. names[0] = "Dave "
3. names[1] = "Kat "
4. names[2] = "Luke "
5. names[3] = "Jon "
6. names[4] = "Rob "
Before we go any further let's remind ourselves of what we've done. We have created a table of information. Each part of the table has a number.
Now let's use a for loop to print the names on screen.
1. array names[5]
2. names[0] = "Dave "
3. names[1] = "Kat "
4. names[2] = "Luke "
5. names[3] = "Jon "
6. names[4] = "Rob "
7. loop
8. clear()
9. for i = 0 to 5 loop
10. print( names[i] )
11. repeat
12. update()
13. repeat
We have a for loop on lines 9 to 11. This is used to print all 5 names on the screen. Without using a for loop, we would need to use 5 different print()
lines to achieve what we want.
The clever part of this is that we use our increasing i
variable in the print()
line. The first time around the loop, the line reads:
10. print( names[0] )
Next time around, i
is equal to 1 so it reads:
10. print( names[1] )
This carries on around and around until we have printed every name from the array.
Recap
A for loop is a loop which repeats a set number of times.
We define a variable in the loop which increases (or decreases) on each repeat.
There are many, many applications for for loops. It's impossible to cover them all here, since we must start simple. However, keep your eyes peeled in the upcoming tutorials and the demo programs in FUZE4 Nintendo Switch to see how else they can be used.
Well done - you're leveling up! See you in the next tutorial!
Functions and Keywords used in this tutorial
box(), circle(), gWidth(), gHeight(), for, loop, playNote() print(), repeat, sleep(), step