Professional Documents
Culture Documents
Variables: Objectives
Variables: Objectives
Variables
Objectives
Students will acquire the following concepts about variables:
• A variable is just a container that holds something inside.
• A value is what a variable contains.
• Variables can have names.
• The value of a variable can change.
• The name of a variable can also be changed.
• Variables can have types.
Chapter 4: Variables
1- Warm-Up Story
47
Chapter 4: Variables
48
Chapter 4: Variables
and Angle.
The following screen will appear: Enter the name “Angle”, then click on OK
49
Chapter 4: Variables
Now you should see your newly created variables in the Variables menu .
From the menu , select the repeat block and implement the following
code:
50
Chapter 4: Variables
4.b- Triangle
Now try to draw a triangle. What should you modify in
the above code to draw a triangle?
51
Chapter 4: Variables
Our goal is to modify the distances in the code to make the car arrive to the destination
Notice that when you press on the green start flag , the car will move as follows:
52
Chapter 4: Variables
Run the code again by pressing on the green flag .The car will move as follows:
Notice that each time we want to try a new distance, we have to change three move
blocks. This is not practical at all. Instead, we can create a variable named distance, and
use it in each move block.
53
Chapter 4: Variables
in the menu .
54
Chapter 4: Variables
• We want to move half the distance when we move down. To do this, click on the
steps below:
• Now replace the last two move block with this new
move block as follows:
• Now try to change the value of the variable distance
to:
◦ 400
◦ 300
• What is the value that made the car arrive at
destination .
55
Chapter 4: Variables
1. Start by choosing a sprite and a backdrop like we saw in the previous chapters.
Here’s a stage example:
2. Now add the blocks to move your sprite using arrow keys as follows:
3. Now we will add our variables. We need four variables, one for counting the
number of moves in each direction (Right, Left, Up and Down).
56
Chapter 4: Variables
First click on the variable menu. Then click on “Make a Variable”. Then name your
variable Up. This variable will count the number of moves Upwards.
We will then create another variable and name it Down. This one will count the number
of moves Downward.
Do the same for the Right and Left directions and create
two variables named: Right and Left.
Now all your variables should appear in the variable
menu.
And your stage will look like the following:
57
Chapter 4: Variables
5. After doing the same for all the variables, your code should look like the following:
58
Chapter 4: Variables
6. Now to reset your variables to zero each time you play the game, add the following
code and enjoy playing the game and counting your moves:
7- Summary
In this lesson, we learned about variables:
• A variable is just a container that stores a value.
• Variables have types.
• Variables have values.
• Variables have names.
• We may store something in the variable to be used for later use!
• Variables can be used to store numbers too!
59
Chapter 4: Variables
8- Evaluation
Exercise 1
Let’s suppose you have three envelopes:
1. The first one is named name and it contains the first name “Samir”.
3. The last one is named score and it contains Samir’s score “A”.
Exercise 2
Given the following variables and their values:
Variables
Values 4 3 1
Compute the following:
• Num1 - Num3 = ______________________.
• Num2 * Num1 = ______________________.
• Num1 * Num1 = ______________________.
• Num1 + Num2 + Num3 = _____________.
60
Chapter 4: Variables
2. There are four bugs in the following code, find them and correct them:
61
Chapter 4: Variables
Use the following blocks to make a code that increases the score each time the cat
touches the basketball.
Exercise 5
Retake the scratch game Move the Car. But now you have a different destination. The
stage is as follows:
62
Chapter 4: Variables
a)
b)
c)
3. Now what would be the number of steps needed to get to the second point?
a)
b)
c)
4. Now by which angle will you turn and which distance will you move to get to your
destination?
a) and
b) and
c) and
63
Chapter 4: Variables
9- Project
We want to make a bounce ball game. The rules of the game are as follows:
3. Each time the player hits the ball with the Paddle he gets 1 point.
4. The game ends when the ball gets past the paddle and touches the red line.
64
Chapter 4: Variables
65