sequence structure

Actions only if something happens

Video - Actions only if something happens

Video Transcript

Good evening.

Many often we want to perform some actions only if something happens that is a certain event occurs.

If this does not happen then it is most likely that we do not want these actions to be performed.

For instance we say:

IF it rains THEN I will open the umbrella

The action "I will open the umbrella" will only be executed if it rains. If does not rain the umbrella probably will not open.

We almost always have to deal with such choices in computer programming.

We only want to execute some commands if a specific event occurs.

This is exactly the subject of today's lesson.

The programming concept described above is the "Selection Structure"

Let's listen to Stevie and what he has to say about this matter



Often when we create scripts, we want some instructions to only run when something occurs.

IF something specific occurs THEN some instructions should be executed.

Are you confused?

I am sure that with the next example you will understand what I mean.

Please display the program that I have prepared ...

Although as you see the script is small - it contains some commands you probably have not seen before.

First let's run the script to see what is happening..

You see how the sprite asks the user.

We have accomplished this with the "ask and wait" instruction from the pallet “Sensing”.

As you see the program waits for the answer.

Once I type my age it will be stored in the variable “answer”.

I did not make the variable "answer". It is a ready-made variable in scratch.

No matter what answer I give - it will go into the box named "answer".

Let me type my age ..

The message "You are a minor" appears. Let's run the script once more.

I will give an age of over 18.

You see that nothing happens.

Observe the instructions now .. The "You are a minor" message is within the IF instruction.

The "you are a minor" message will only be shown if the answer is less than 18.

As is the case. You saw that it is not running if the value is greater than 18.

Let's change the script so that the message "You are an adult" appears in the corresponding case.

We still need an IF instruction to check the other case.

Let's run the script now.

You may have noticed that by entering the value 18 no message appears on the screen.

Try in your own scratch window another IF or other change in the code in order to fix this little gap in the Script.

Instead of using a lot IF instructions you could use the instruction IF - then ELSE.

Watch the same scenario in the next few seconds but with the command IF then ELSE.

I think you understood it and became wiser with this video, isn’t that right?