Back to: Scratch Programming
Let’s write some short programs to see what some of these code blocks do.
Start a new Project – we are going to save it as “Hello You”. In this program we will have the code interact with the user.
- We’ll start the program by clicking the GREEN flag. Drag it over.
- Next, use the “ask” code block from the sensing area. Default asks “What’s your name?”. We can ask any question, then the program will wait for user input. The user input goes into a variable called answer. (i.e. answer = Bob)
- We’ll have the cat say “Hello, Bob” by using the “say” code block from looks, we’ll also need the “join” code from operators the join our two words, finally we’ll need “answer” from sensing to be our second word. Just stack them.

looks like this after stacking them:

Your program should look like this so far:

Add one more line of code to make the cat think “Wow! Shane seems like such a nice person!”

Let’s build on our program so we can have a short conversation with the cat.
The cat is going to ask the user how old they are. The response from the user will determine how the cat reacts. This is called a conditional statement where the program will test if the condition is true. if it is, it performs one way, if false, it performs another way.
Try this!
Enter and complete the example program below.
Add code to make the program calculate and display “You were allowed to start driving <???> years ago”.

Variables
You should remember variables from algebra class, like x and y variables. Variables are just containers holding a value that can vary (hence the name).
In programming, the concepts are similar, we name our containers and put some values in them. Later when we want to refer to them, update their value or get their current value, we use that container’s name (the variable name). We can imagine variables as boxes. We can put something in that box, take it out, and put something else. In programming it’s similar. We store some value in the variable. We can call that value or we can change it and store some other value.
Let’s use a real-life example. Imagine apple sellers on your local farmer’s market. They come to the market with a certain fixed number of bags of apples. We can say that apple’s count is a variable initialized to some starter value, like 10.
Our seller starts attracting his customers by yelling: “Fresh apples, only 10 bags left!”. If he sells one apple, there will be only 9 bags left, so he will yell “Only 9 left!”. We updated our variable and now we can retrieve its value and use it as needed.
Moving the sprite
In computer graphics, a sprite is a two-dimensional bitmap that is integrated into a larger scene, most often in a 2D video game. Originally, the term sprite derived from the fact that sprites “float” on top of the background image without overwriting it, much like a ghost or mythological sprite. In Scratch, the default sprite is a cat.
Start a new Project – we are going to move the sprite (cat) around the stage. In this program we will have the code move the cat in a circle and to various locations.

The first of these four small programs needs to be run multiple times to complete a circuit. Each subsequent program builds on the first making some improvements.
What improvements are evident in program 2, 3 and 4?
Loops and Iteration
In the example above we’ve been introduced to the loop. When we want to repeat something a certain number of times, we use loops. We use them all the time, in programming, as well as in everyday life!
Loops consist of three main parts:
- Initialization – starting the value of our variable.
- Condition – specifies how long the loop will run.
- Update – we must modify the variable’s value so that the loop will eventually break, otherwise, we will produce a dreaded infinite loop.
Applying this action to our apple seller. He came to the grocer’s market with a certain number of apples, say 10 bags. We can use that variable as an initialization value for our loop. His wife told him not to come home until he sells all the apples he brought. Gulp!
So selling all the apples is the condition that will break this loop. He will continue selling apples while he has more than 0 apples. Every time he sells 1 bag of apples, he will go through the whole process, greeting customers, picking apples, packaging and so on. Apple count variable will be updated by -1 in each round and in the end, the condition will no longer be true ( apple count > 0). He can finally go home! In programming that means we break out of the loop, that the loop is done, stops running, and code outside of the loop can continue.
People really hate to do repetitive things, but thankfully computers are great at that and don’t mind it at all! Now that you have variables and loops in your toolkit, you can take advantage of that, and save your time for more fun stuff. With this new knowledge, you can start making really interesting programs and share them with your friends.
Costumes – making the cat walk
Click the “Costumes” tab. By default you should see two cat images in the left of screen. Click them one after the other and you’ll notice that it gives the appearance of the cat walking.

Click back to the “Code” tab. We are going to program the cat to walk across the screen.

Make a note of what each line of code does.
Backdrops and Sprites
In the bottom right corner you’ll find a “Choose a backdrop” button. Clicking this button will show you a library of available background images that you can use in your projects. Have a look through them and choose one.

Assignment
Create code to make the cat start at the top centre of the stage. The cat needs to run in a circle by moving 35 steps then turning right 15 degrees and continuing. If I press the “SPACE” bar, the cat should stop and say “Goodbye!” then the program should stop running.