Tutorial 2: Variables

Variables are another of the most important parts of programming.

What are they? Well, put very simply a variable is a label. A label you create to store a piece of information.

Why do we need labels? Well, imagine you were trying to find a needle in a haystack. Quite the challenge, right? Not if we had a nice big signpost telling us exactly where to find it!

The main reason why we use variables is that they allow us to change and manipulate things in a program.

Almost every single game uses variables all the time, to keep track of everything from a score to health to stats to screen position to the controller buttons and so on.

Let's get started. I hope you like sweets!

  1. sweets = 3

Type the code above into the FUZE4 Nintendo Switch code editor and press + to run the program.

Nothing will happen at all. In fact, you'll be taken instantly back to the editor!

However, in the computer's brain we have taken the number 3 and stored it in a place called sweets.

Now, change your program so that it looks like the one below and run.

  1. sweets = 3
  2. print( sweets )
  3. update() 
  4. sleep( 2 )

All we are doing is instructing FUZE to print the contents of the sweets variable on screen. You should see the number 3 appear! FUZE is finding the information labeled as sweets, and printing whatever it finds.

We can store anything in a variable. The code below will also work:

  1. sweets = "Delicious"
  2. print( sweets )
  3. update() 
  4. sleep( 2 )

This time though, we'll get the word "Delicious" instead of the number 3. Make sense? Great!

Let's make this a little more complicated. We'll be using a couple of new, very important things.

Change your code in the FUZE4 Nintendo Switch editor so it looks like the program below and run it. It should count down our sweets until we have none left.

  1. sweets = 3
  2. while sweets > 0 loop
  3.     clear( black )
  4.     print( "I have ",sweets," sweets in my bag." )
  5.     print( "If I eat one... then... " )
  6.     sweets -= 1
  7.     update()     
  8.     sleep( 1 )
  9. repeat
 10. print( "I have no sweets left... Oh no." )
 11. update()
 12. sleep( 2 )

First things first - we have a brand new function here. clear() is used to clear the screen with a colour. Try putting a different colour in the brackets.

Notice that on line 4, we have the variable name sweets between commas, not as part of the text. If we were to say {code:print("sweets")} we would get the word "sweets". See the speech marks in the brackets?

But remember that when we say print(sweets), with no speech marks, we get the contents of the variable.

Let's talk about the new things we've introduced here. The first one is a new different type of loop called a while loop.

While

A while loop is a conditional loop, which repeats until a certain condition is met.

This while loop repeats as long as the sweets variable has a value greater than zero (sweets > 0).

For our while loop to actually stop and move on with the program, we must reduce the value of the sweets variable to 0.

The next tricky part is line 6.

Minus equals (-=) and Plus equals (+=)

The line of code which reduces the value of the sweets variable is line 6.

  6. sweets -= 1

The sign after sweets is called minus equals. We're going to see these signs a lot as we move forward, so let's do our best to understand them. -= means we are subtracting from the value stored in our variable. += or plus equals is adding to the value.

Really, line 6 actually reads:

  6. sweets = sweets - 1

This means: Redefine the sweets variable to be equal to whatever is currently in the sweets variable minus 1.

Using -= and += helps us save a lot of time because we don't need to write every variable name twice!

Still with us?

Awesome! Using variables becomes second-nature eventually, but it can be a bit strange to get used to at first.

Try to rewrite the program so that instead of taking sweets away, you begin with 0 sweets and gain them as the loop continues. You should change the while loop too so that the loop exits when you reach a certain number of sweets.

Well done! You reached the end. See you in the next tutorial where we'll talk about another one of the core programming techniques - If Then Statements.

Functions and Keywords used in this Tutorial

clear(), loop, print(), repeat, sleep(), update(), while