Download presentation
Presentation is loading. Please wait.
Published byClaude Fox Modified over 9 years ago
1
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop
2
Session 4
3
Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs later in the program. There are many different types of variables in Alice, but we will only be covering three in detail: Numbers – A number variable can contain almost any value. (e.g. 1, 0.01, -5,+/- 10 million if not more) String – A string variable can contain letters, words, or sentences.(e.g. “s”, “string”, “This is still a string”) Boolean – A Boolean variable contains a true or false value.
4
Number Variable Overview – A Video Game Outlook Incrementing/Decrementing Number Variables – This is useful when decreasing or increasing variables by a specified number. (e.g. a player losing lives in a game or increasing a player’s score). Setting Number Variables – This tool is used to set the variable’s value to a specified number. (e.g. setting the score to zero after the game is restarted or setting the players lives to max after the game is restarted) Using Number Variables – Programmers use variables to store data for later use. Problems/Type differences – Similar to the issues related with input, different types of data may have unexpected outcomes.
5
World Variables and Object Variables Variables can either be world variables or object properties. World Variables - Use these variables when all objects will be sharing one variable. For example, in football, it would be inefficient for each player to keep track of the time remaining in the quarter using their own clock, it makes sense to have one “global” clock that everyone can see. Object Properties (Variables) – Use these variables when an object requires its own variable. A good example might
6
Incrementing/Decrementing Number Variables As discussed in session 3, it is possible to use a variable inside of a while loop in order to make it behave like a regular loop. The steps required to construct this also help us understand how to increment variables. Create a new project and get a loop ready to repeat twice. Now drag a camera “move” block and place it inside the loop and choose a direction and distance. Now drag a while loop and place it below the previous loop. It doesn’t matter if you select true or false, we will be changing that promptly. Go to the properties tab of the camera object and create a new variable named “index,” make sure it is a number variable and the value is 1.
7
Variables in the Object Details Area There are three things we can realize about a variable when looking at it in the object details area. By this icon we can observe that this variable is of the type “number”. String and Boolean variables have a similar representation. This represents the name of the variable. It is possible to change the name by right clicking it and then entering a new one. Here we can see the value the variable contains when the program starts. It is possible to change this value by clicking the arrow next to it.
8
Incrementing/Decrementing Number Variables Make sure the value of the variable is 0 when the program starts. Now drag a < operator from the world’s functions, place it into the conditional part of the while loop, mouse-over expressions, mouse-over camera.index, and select 2. Since this while loop will execute “while the index variable is less than 2” and the index variable never changes inside the loop, we need to increase it by one each time. This gives it the same effect as a regular loop. Drag the index variable and place it inside of the while loop where the “Do Nothing” is located. Now choose to increment that variable. Alice can only increment/decrement variables by 1 but it is possible to construct a snippet which will increment/decrement a variable by a value other than one.
9
Closer Look Look at the regular loop we have in our methods editor, there should be a button which you should click. This can help us better understand that both of these loops are identical. Loop/While Comparison – “index from 0” means the index starts at 0 (This number can be changed by clicking the arrow next to 0).“Up to but not including” translates to “while index is < number.” Incrementing by 1 is easy enough to understand. Drag a camera move block to the while loop and choose the opposite direction from the move block previously chosen. Choose the same distance you chose previously. Now let’s test our program. If you chose a large distance, it may take longer for the camera to move but it should move one way using the first loop and move the exact opposite way with the other loop. This is to show these two loops can be constructed to behave the same way. Now try to construct another loop using a while loop that is identical to the previous while loop except it decrements the index variable. (Hint: You may need to play with values in the conditional part of the while loop.
10
Setting Number Variables Let’s now save the world and create a new one. The next example will be very simple because it is so similar to an example done in session 1. Add an object and create a variable for that object named “distance.” Choose 0 for the initial value. Drag the variable to the methods editor, mouse-over “set value” and choose 1 for now. Drag a “ask user for a number” block and place it in the box after set value to, choose other and type “Enter a distance for the object to move.” When the program starts, the “distance” variable will be set to whatever the user’s answer is. Now drag a move block from the object’s methods tab, choose a direction and distance to move. Now replace the distance you just chose with the “distance” variable. Let’s test our program and see what happens.
11
String Variables Words can also be used and stored into variables. Examples could be asking someone's name, or what their favorite color is, or if they would like to continue with the program. The example we will construct will modify the previous program to also ask the user if they would like the object to continue moving, if so, to enter the word “continue.” This will require us to create a new variable named “answer.” Even though a world variable isn’t required here, let’s go ahead and make this variable a world variable. When it asks for a value, select other and type “continue.” Drag a while loop under the current snippets already present, select true. Now drag the “answer” variable we just created and place it in the conditional part of the while loop, mouse-over ==, and select “continue”. If “continue” isn’t present, choose other to input it yourself. Now drag the object “move” block to the methods editor and place it inside the while loop. Drag the “answer” variable from the properties tab of the world object and place it under the move block but still inside the while loop and set the value (doesn’t matter what the value is for now). Drag an “ask user for String” block and place it in the box after the words “set value to” in the set answer block (world.answer), type your own question, it could be something like “Enter continue for the object to move again.”
12
String Variable Test When the program reaches the while loop, it first checks if the “answer” variable is equal to “continue.” This is why we set the initial value of the answer variable as we did. Once in the while loop, the object moves, then the answer variable is set to the user’s response to your question. Again, the conditional in the while loop checks if the “answer” variable is equal to “continue” and acts as it did before.
13
Problems that could Arise In the program we have created, when the user is asked a distance to move, try entering a word or letter instead of a number. Test this and see what happens. The cause? Alice is type sensitive meaning it has different types which are incompatible with each other. Usually Alice will not allow us to mix types; for example, if we were to create a number variable, we couldn’t assign a string value to that variable. However, if the user enters a number value when asked for a string, Alice can’t really stop them from doing it but it will be stored as a string instead of a number. In Java or C++, this could cause an error that could easily be fixed, but the designers of Alice accounted for the error so it most likely will continue running. However, you may have unexpected results. The good thing about entering a number instead of a word is Alice will actually print the numeric value as a word. This could be quite useful.
14
Boolean Variables Boolean variables can be placed in the conditional section of a while loop and in the predicate of an if/else structure. Right click the while loop and click disable. Drag another while loop and place it above the disabled one. Create a world variable named “boolean” of type Boolean and set the variable directly above the enabled while loop and choose true. Drag a “ask user for yes or no” block and place it into the “set” block; for the question type “Would you like the object to move again”. Now drag the Boolean variable and place it in the conditional part of the enabled while loop. If the value of this variable is true, the while loop will repeat forever, if it is false, it will never enter the while loop. Now drag the move block from the disabled while loop and place it in the enabled while loop. What do you think will happen when the program starts? If the user answers yes, the object will move forever, if the answer is no, it won’t move at all. This is exactly how a Boolean variable works in programming as well.
15
Project 4 Create a Alice script that averages multiple numbers. You can either ask the user to enter how many numbers they would like to average and use a loop, or use a while loop and wait for the user to enter “continue.”(There are many other ways to do this, feel free to be creative, this is only two ways of giving the user a way to stop the program.) Create an Alice program that asks the user to enter 2 numbers, stores them as variables A and B, then switches the values in the variables.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.