You are on page 1of 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

holds either true or false (c) Object . Good programming practise requires that each variable always start with a known value.holds an Alice object (d) Other . Always initialize your variables. Sound. such as String. (c) Class-Level Variables are like the properties that belong to specific classes (and objects). Data Type . Color. there are four data types. In Alice. When the end of the method is reached. it is like the variable does not exist. All objects in the world can see their values. and each data type will generally serve a specific purpose.Variables in Alice 2. By default. Alice will usually fill in a value for you.can hold any number (integer or real) (b) Boolean . (d) World-Level Variables are properties that belong to the entire world. the variable ceases to exist. They can be set or modified before or during program execution. but don't take this for granted.Computer programs can use and store many different types of data. Scope / Category . the move to method allows you to specify another object as the destination. (a) Local Variables belong to a specific method.there are several other special types of data that can be used in Alice. and any method in the world can use or modify the variable. (b) Parameter Variables are used to hold an argument that is being passed to a method.The scope of a variable puts limits on where a variable can be used in a program. and you can only store data of the appropriate type in a given variable: (a) Number . 3. A local variable can only be used inside the method where it is defined. Page 2 of 4 . For example. etc. Initial Value The initial value is the starting value placed in the variable. This target object is specified using the parameter variable of the move to method. Outside the method.

Create a new world with an instance of the Penguin class (Animals). Save this world as PenguinTutorial01. What do you think should happen? Play to world to confirm your guess. Select the Penguin object and find the Method called jump. 3. 2. Drag this method into your programming window.Variables in Alice Exercise 1 – Creating and Using a Variable 1. Create a new variable called numberJumps. and set the times parameter to be your new variable (numberJumps). 4. Initialize numberJumps to 5.a2w Page 3 of 4 . 5. numberJumps should be have the Number data type.

jump method. and put the copy at the end of your program. 4. drag the variable tile (at the top of the programming window) to the location in the program where you want to make the change. If you want to change the value. Set the value to 2.jump method that should already be there from Exercise 1. Exercise 2 – Changing a Variable using the Set Instruction 1. Copy the penguin. Save this world as PenguinTutorial02. To create a set instruction.a2w Page 4 of 4 . it will keep that value throughout the execution of the program. Drag the variable numberJumps into the programming window. below the penguin. Execute your program. you can use a set instruction to specify a new value. 2. which already refers to the numberJumps variable.Variables in Alice Changing Variables Using the Set Instruction Once you have given an initial value to a variable. 3.