Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

CHAPTER 4

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

2- Think and Discuss


Let’s call the container a variable.

What does the following variable


(container) contain?
What is the name of the variable?
How many flowers does it contain?
Which color (type) does the variable
(container) contain?

3- Class Activity: Pens in Envelopes


Take out some colored pens, then group pens with the same color inside one envelope. Do
this for each color.
Discussion
In this activity “Pens in Envelopes”:
• What are your variables? _____________________________________________.
• What are your variables names? __________________________________________.
• What are the types of your variables? _____________________________________.
• Can you add the content of two envelopes of different types?___________________.
• Can you change the value of your variables? How? _________________________
• ____________________________________________________________________.

48
Chapter 4: Variables

4- Application: Draw Geometric Shapes


4.a- Rectangle
Now we want to draw some shapes like a square, rectangle, triangle, ...etc
We want to use variables to store the side lengths and the
turning angles.

Select the menu and create two variables named Length

and Angle.

Then create the following code using the Pen extension .

Click on the variables menu. Then choose .

The following screen will appear: Enter the name “Angle”, then click on OK

Then click again on and create another variable named length.

49
Chapter 4: Variables

Now you should see your newly created variables in the Variables menu .

Now, form the menu, choose and .

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?

The triangle should look like this one

5- Scratch: Move the car


Open Scratch and open the file “Move the car.sb3”. You’ll see
the following stage and code.

51
Chapter 4: Variables

Our goal is to modify the distances in the code to make the car arrive to the destination

. To get to the destination, the car should move as follows:


1. Move forward a certain distance.
2. Turn right 90°
3. Move forward distance/2
4. Turn right 45°
5. Move forward distance/4

Notice that when you press on the green start flag , the car will move as follows:

Why did the car move like that?


Now let’s try modifying the distance in each of the following blocks:

52
Chapter 4: Variables

Run the code again by pressing on the green flag .The car will move as follows:

Unfortunately, the car didn’t get to the finishing point!

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.

To do so, click on the variables menu. Then choose .


The following screen will appear: Enter the name “distance”, then click OK

53
Chapter 4: Variables

Now you should see a block named “distance”

in the menu .

• First we will set a value to the variable distance.


Let’s set it equal to 200

• Then we will place the distance variable


inside the first move block as follows:

54
Chapter 4: Variables

• We want to move half the distance when we move down. To do this, click on the

menu and choose the division operator , then follow 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

6- Count the moves


Now we are going to make a simple game in which you can count the number of moves
for your sprite.

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

4. Now we want to modify each variable by pressing on the corresponding key:

a) Change the variable Right when we press on the right arrow.

b) Change the variable Left when we press on the left arrow.

c) Change the variable Up when we press on the up arrow.

d) Change the variable Down when we press on the down arrow.

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”.

2. The second envelope is named class and it contains “Grade 5”.

3. The last one is named score and it contains Samir’s score “A”.

Complete the following sentence:


In today’s exam, my friend name from the class got a score.
In today’s exam, my friend ……….. from the …….. got a ……....
Now we modify the contents of the envelopes to the following:

1. name contains “Sarah”.

2. class contains “grade 7”.

3. score contains “A+”.

Now complete the sentence again:


In today’s exam, my friend ……….. from …….. got 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

Exercise 3: Find the bugs


1. Find and correct the bug in the following code:

2. There are four bugs in the following code, find them and correct them:

Exercise 4: Construct your blocks


Set up the following stage.

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:

1. Create a variable named distance.

62
Chapter 4: Variables

2. What would be the distance if we want to move the


car to the following position?

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:

1. The player controls the Paddle.

2. The ball bounces on the screen.

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

Block Menu Functionality

The block will make its sprite continuously pen a


trail wherever it moves, until the Pen Up block is
used.

If a sprite is currently using the pen because of


the Pen Down block, the block will cause the
sprite to stop drawing a trail. (Otherwise it has
no effect.)

The block will play the specified sound


without holding up its script .

If the block's sprite is shown, it will hide the


sprite — if the sprite is already hidden,
nothing happens.

If the block's sprite is hidden, it will show


the sprite — if the sprite is already showing,
nothing will change.

65

You might also like