Вы находитесь на странице: 1из 4

Variables in Alice

Definition: A variable is a named storage location in the computer's memory. As programs become more useful and more complicated, they need to keep track of specific information as the program runs, and later refer to this information as needed. The most common place to store this information is in the computer's memory (RAM) using a variable. Creating a Variable To create a variable, the programmer specifies the name and the data type of the variable. It is also important to consider the scope of the variable, which is determined by where you create it.

1. Name - Choose a meaningful name for the variable that will allow you to easily remember what the variable is for. Remember that you might not be the only person who has to read and understand the program, so name your variables for a wider audience.

Page 1 of 4

Variables in Alice
2. Data Type - Computer programs can use and store many different types of data, and each data type will generally serve a specific purpose. In Alice, there are four data types, and you can only store data of the appropriate type in a given variable: (a) Number - can hold any number (integer or real)

(b) Boolean - holds either true or false


(c) Object - holds an Alice object

(d) Other - there are several other special types of data that can be used in Alice, such as String,
Color, Sound, etc.

3. Scope / Category - The scope of a variable puts limits on where a variable can be used in a
program. (a) Local Variables belong to a specific method. A local variable can only be used inside the method where it is defined. Outside the method, it is like the variable does not exist. When the end of the method is reached, the variable ceases to exist. (b) Parameter Variables are used to hold an argument that is being passed to a method. For example, the move to method allows you to specify another object as the destination. This target object is specified using the parameter variable of the move to method. (c) Class-Level Variables are like the properties that belong to specific classes (and objects). They can be set or modified before or during program execution. (d) World-Level Variables are properties that belong to the entire world. All objects in the world can see their values, and any method in the world can use or modify the variable. Initial Value The initial value is the starting value placed in the variable. Good programming practise requires that each variable always start with a known value. By default, Alice will usually fill in a value for you, but don't take this for granted. Always initialize your variables.

Page 2 of 4

Variables in Alice
Exercise 1 Creating and Using a Variable 1. Create a new world with an instance of the Penguin class (Animals).

2. Create a new variable called numberJumps. numberJumps should be have the Number
data type. 3. Initialize numberJumps to 5.

4. Select the Penguin object and find the Method called jump. Drag this method into your
programming window, and set the times parameter to be your new variable (numberJumps).

5. What do you think should happen? Play to world to confirm your guess. Save this world as PenguinTutorial01.a2w

Page 3 of 4

Variables in Alice
Changing Variables Using the Set Instruction Once you have given an initial value to a variable, it will keep that value throughout the execution of the program. If you want to change the value, you can use a set instruction to specify a new value. To create a set instruction, drag the variable tile (at the top of the programming window) to the location in the program where you want to make the change. Exercise 2 Changing a Variable using the Set Instruction

1. Drag the variable numberJumps into the


programming window, below the penguin.jump method that should already be there from Exercise 1. 2. Set the value to 2.

3. Copy the penguin.jump method, which


already refers to the numberJumps variable, and put the copy at the end of your program. 4. Execute your program. Save this world as PenguinTutorial02.a2w

Page 4 of 4

Вам также может понравиться