Hey. Nice to see you again. It’s Christy and I’ll be your instructor for this lesson. Today is a special day. Do you know what today is? “Happy Birthday to you. Happy Birthday.”
No. Not my birthday. It's the day we learn about variables!!!! So, Lets Code!
Variables are used to store information for use in the computer program. Variables allow us to lgive a name to our data, so our programs can be understood more clearly by programmer. This is done by creating a label and a location in the computer’s memory where we can store the data.
Variables can change values, but the name or label stays the same. That is what makes them so useful.
Lets discuss a couple examples and then see how we create a variable in Scratch. I am going to use examples from my favorite sport and my favorite team – Cricket and the Rajasthan Royals. I know some of you in Asia and the Americas do not follow cricket. That is Ok. You will still be able to follow.
The Rajasthan Royals have asked me to write a program in Scratch to track the runs in each match. I create a variable and give it the name “Score”. I set the original value to zero.
Each time a run is scored update the score by pressing the right arrow. If I make a mistake, I can adjust the score by pressing the left arrow.
Now, the match starts. The Royals are at bat. The variable Score = 0. The Royals score a run. I press the right arrow key and the program updates the variable by one run. Score = 1
The batsman hit a boundary. The Royals score 4 runs. I press the right arrow key 4 times and the program updates the variable. Score = 5 Pretty easy to understand right?
Here is another example. In this example, we want to create 2 variables xPosition and yPosition. These will keep track of the X and Y positions as we move us-ing the 4 arrow keys. The logic is similar. We set the initial values to (0,0), which is the center our screen. If we move the Sprite, the program changes the variables to track the position.
Onto the exercises and projects .I know you have this down. See you next lesson and Go Royals!