Scratch Programming
Selection

Introduction

Selection is a very important concept in programming. Using selection statements, we can get our programs to make decisions.

Example Program 1 - Passing The Test

In a school test, you need to score more than 20 marks to pass the test. The program asks the user to enter a test score. If they score more than 20 marks, the user is told that the test has been passed. The program has a variable called score.

The scripts you will need to do this are as follows,

Scratch Program

The most important block here is the the If block. The statements inside this block are only executed if the score is greater than 20.

Challenge: Change the program so that it asks for your age. If the age that is entered is 18 or over, tell the person that they are an adult.

Example Program 2 - Passing Or Failing The Test

In a school test, you need to score more than 20 marks to pass the test. The program asks the user to enter a test score. If they score more than 20 marks, the user is told that the test has been passed, if not they are told that they have failed. The program has a variable called score.

The scripts you will need to do this are as follows,

Scratch Program

Challenge: Change the program so that it asks for your age. If the age that is entered is 18 or over, tell the person that they are an adult, otherwise tell the person that they are legally a child.

Example Program 3 - Entitled To Drive

A person can legally drive a car if they are 17 or over and have passed their driving test. The program asks the user for their age and if they have passed their test. These two pieces of information are used to determine if the person is allowed to drive or not.

The scripts you will need to do this are as follows,

Scratch Program

The key part of this program is the AND block.

Challenge: Change the program so that it asks for your age. If the age that is entered is from 13 to 19, tell the person that they are a teenager, otherwise tell them they are a normal human.

Example Program 4 - The Correct Input

A program has a menu of choices. The user must enter a number from 1 to 5 in order to access the menu choices. If the user enters a number lower than 1 or higher than 5, the program tells them they have made a mistake.

The scripts you will need to do this are as follows,

Scratch Program

When you test this program, you might notice that it doesn't work properly if you enter letters. The key part of this program is the OR block

Challenge: Adapt the program you wrote in response to the previous challenge so that it uses an AND block instead of an OR block.

Example Program 5 - Nesting Your Statements

A program asks the user to enter 2 numbers. Depending on the numbers that are entered, one of the following messages is output,

The two numbers are equal
The first number is larger than the second
The second number is larger than the first

The scripts you will need to do this are as follows,

Scratch Program

Notice that you have an IF...ELSE block inside another IF...ELSE block. This is called a nested IF.

Challenge: Write a program that asks the user to enter a number. If the number is positive, work out if the number divides exactly by 3 (you will need to use the mod block to see if there is a remainder). Get your program to tell the user if they have entered a positive number that divides exactly by 3.

Example Program 6 - Sensing

A program has a sprite which follows a track around in a continuous circuit. When the sprite touches the area outside of the track it turns back towards the track.

Here is a screenshot of the program at the start.

Scratch Program

The track is the stage background. The car's wheels are all different colours. Here is the script that makes the car go around the track,

Scratch Program

You can download this scratch program to test and adapt. example 6.sb. Try to explain in your own words how the script makes the car stay on the track.

Adapt & Extend The Program

Keyboard Input

Add a variable called speed and make it for all sprites. Add an If block to check if the 1 key is pressed. If it is, set the speed to 1. Don't forget to make it so the car moves speed steps.

Test this feature and then add more blocks ot make the numbers up to 5 change the speed. Also add 0, so that you can stop the car.

When this is done, try to make it so that the car can be made to speed up and slowdown. Use the change blocks in the Variables section.

Car Races

There is a block in the operators section that allows you to create a random number. If you used a random number of steps (1 to 5), the car would be speeding up and slowing down all the time. If you made the car smaller and duplicated it, you could have two cars racing.

Add a sprite to act as a finishing line and you can have a race. You will need to create some scripts to work out which car won the race.