Variables
Used on 9/15/25
Used on 9/15/25
In coding, variables are considered containers that hold a value that can be retrieved or called upon.
This data can be a string (A - Z, 0 - 9, Special symbols etc.), a number (-infinity to infinity), or a boolean value(true or false)
Variables are important to coding because they allow us to store and retrieve data dynamically.
Let's explain the code blocks
The variables are all circular, and we only begin with my variable, as we add more variables, they'll be listed here. By clicking the box, you can either hide or display the variables to users
set VARIABLE to ( ) will set the variable to an assigned value
change VARIABLE by ( ) will change the variable by the value assigned
show variable will display the variable to the user
hide variable will hide the variable to the user
For our example today, we will create a variable called timer. The timer will store a number that will represent the number of seconds we decide on.
How exactly do we start this? We must first initialize the variable by creating the variable and assigning it a name and value
Let's begin by creating the variable name
Navigate to Scratch. Make sure the Code tab is selected. Click on the variable button.
Next, click on the Create a variable button. You will see the menu on the left. You can give the variable a name, assign it to either all sprites or this sprite only. You can also store it on the server.
For this example, I gave the variable the Timer name (you can pick a similar name) and I selected For all Sprites. Click OK when done
You will notice our new variable on the left side menu.
Let's assign a value to the variable. Look at the code on the left side.
This basic code will assign the value of 60, an integer, to the Timer.
Now we can change the value by using the change variable by ( ) block. We will make the value go down from 60, to 0 in the next step.
The code on the left features variables and loops. What exactly is a loop?
A loop is a statement that allows a block of code to be executed repeatedly based on a condition. Loops are fundamental to programmers, because they enable the automation of repetitive tasks. (imagine having to write a long piece of code 99 times?)
In Scratch, whatever code is inside a loop will occur as long as the loop condition is true. In this case, our code will repeat 60 times and then stop after the 60th time.
Our code will assign a value of 60 to the timer once we click the green flag. Afterwards, we will begin a loop that will repeat 60 times. It will wait 1 second, and then change our Timer variable value by -1. By the end of this code, we will have a Timer with a value of 0
We just created a basic 60 second timer! We can modify this code to make it more complex as well. Let's look below
The code on the left will begin when we click the green flag.
We will show the Timer variable to the user, and then assign the value of 60 to it
The code inside will repeat until our Timer variable is equal to 0. (Unlike the other code, where we would have to change the repeat block value to match the timer value.)
It will wait 1 second, then change the value of Timer by -1.
If the Timer's value is equal to 30, then our sprite will say Halfway There
Once the Timer value is equal to 0, our sprite will meow and then say TIME IS OVER!
You can see how a few changes have made our timer more efficient!
Now, let's add modify this code to create a fun project
The code for the cat is in two parts. Most of the code on the left side is still the same, except for a few changes
We removed the Green Flag event, and replaced it with a new Event Block. When I receive STOP is what I named this event. This will only occur if a message named STOP is broadcast by code
The first say block now says ALMOST THERE!!!
We added a "stop other script in sprite" block after the loop is broken. This will stop any other script inside the sprite (the script that's forcing our poor cat to fly around!)
Finally, we changed the 2nd say block to say Phew, you saved me!
The Block on the right is all new code that begins when we click the green flag
We begin by making the Cat sprite go to X = 0, Y = 0. This means the cat will be right in the middle
It will hide the timer variable
The Cat will then say PRESS THE TIMER AHHH!!!! for half a second
The cat will then forever go to a random position.
We have no way of saving our cat friend! If only there was a way to save him!
I created a new sprite by selecting the Timer sprite in the Scratch library. I then added the words Timer using the text tool!
This code will begin when we click on the timer sprite. This handy sprite will save our cat friend!
It will broadcast the STOP message to other sprites. This directly works with the cat sprite and will start our timer code!
It will switch costumes to b, then wait half a second and switch back
Don't press the button again or it will reset the timer for our poor cat!
I wonder if there's a way to fix that? (There is, think about how it's possible!)
Quick Review of what we learned today!
We learned about variables (they're containers that store values and they can be retrieved or called upon)
We learned how to create variables in Scratch, and how to initialize them by assigning them a name and value
We learned how to create a basic timer in Scratch
We learned how to broadcast messages to set off chain events!
We saved our cat!