Using variables and operators
Variables are essential for programs that maintain state information or that work with input received from sensors. In this lecture you will learn how to create and use variables. For example you will learn how to increase a counter variable on a button sensor event. Another program will be written to process events received from the Infra Red (IR) sensor with the help of variables.
Figure 1 - How to using variables and operators
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- SNAP: http://www.ozeki.hu/index.php?owpn=6180
- Sniffer: http://www.ozeki.hu/index.php?owpn=6204
What is the variable?
Variable - Variables are names that you give to your computer's memory locations. Variables can store numbers and strings and you can use them in your programs to make operations with them.
Discover 'Variables' blocks in SNAP
In this task you will learn where you can find variables. Open the 'Variables' category and you will see every block about variables. You can create variables if you click on 'Make a variable'. You will see a pop-up window where you should give the name of the variable (Figure 2) then it will appear in the list. After that you can set it's value, change it's value and show and hide the variable. You can also find blocks about list here. Let's see the next step and learn what variables are good for.
Figure 2 - Variables in SNAP
Discover 'Operators' blocks in SNAP
In this task you will see where you can find operators and how you can use them. Open the 'Operators' category. You can see blocks about operations like addition, subtraction, multiplication, division and if you have variables you can do that operations with them. There are other operators like comparing, and, or, not, etc. In Figure 3 you can see how you can use a variable in an operator.
Figure 3 - Operators in SNAP
Write a program that increase a counter with button presses
In this task you will write a program that can increase your counter if you press a button. You need to create a variable called 'counter'. You will increase that variable. You should use the 'when [any key] key pressed' block to start the program and then with 'change [counter] by ' block you can increase the variable (Figure 4). If you press any key on your keyboard the counter will increase with 1. It is a simple program to see how you can use variables.
Figure 4 - A program that increase a counter
Write a program that double a counter
In the previous task you have written a program that can increase the counter if you press any key. Now, you will complete that program. In this task you will write a program that can increase the counter if any key has pressed and double the counter if the object proximity less than 10. Check the program in Figure 5 and write it in SNAP to see how the program works.
Figure 5 - A program that double a counter
Wirte a number game program
Now, write a program that add two random numbers and set the value to 'sum' variable. The program shows you the addition then you will type the result. If you typed the right value (the 'sum' and 'answer' are the same) you will see a happy face on the Brick's screen (Figure 6). You need to create two variables and use operators like '[variable] + [variable]' and 'pick random  to '.
Figure 6 - Program about the number game
Download the programs|
The first program is capable to increase a counter if you press a button. The second program is capable to double a counter if the object proximity less than 10. The third program is capable to build a number game.
Download Ozeki SNAP code