Course 2 / Lecture 6:

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

Requirements


Task #1:

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.


Task #2:

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


Task #3:

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


Task #4:

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 [1]' 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


Task #5:

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


Task #6:

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 [0] to [25]'.


Figure 6 - Program about the number game

Program code


More information


Next


Copyright © 2000- |Ozeki Ltd | info@ozekirobot.com |
Page: 6196 | 79.99.42.43 | Login