Lesson video

In progress...

Loading...

Hello everybody.

It's Mrs. Franzsen and here.

Welcome to Computing.

It's really good to have you in my lesson today.

Let's get started with our learning all about physical computing.

By the end of this lesson, you will be able to use a loop to repeatedly check whether a condition has been met.

Let's look at our keywords for today.

Our first keyword is condition.

A condition is a statement that can be either true or false.

When we are using the words true or false in a condition, you can see that we use a capital letter for true and a capital letter for false.

Our next key word is selection.

Selection is a part of a programme where if a condition is met, then a set of commands is run.

In the first part of the lesson, you are going to use if, then to control programme flow.

Identify the condition in this algorithm.

What will happen when the condition is false? What will happen when the condition is true? Pause the video and have a think about that.

Okay, so the algorithm reads, do until I hear a clap, tap my nose with my thumb.

Where is the condition? The condition is until I hear a clap.

What will happen when the condition is false? If the condition is false, tap my nose with my thumb will happen.

If the condition is true, then you stop the action.

Well done if you got that right.

Identify the condition in this programme.

What will happen when the condition is false? What will happen when the condition is true? Pause the video and have a think about that.

Welcome back.

Let's look at the condition in this programme.

Where is the condition? The condition is do until A is HI.

So A is HI in Crumble programming means the button is pressed.

So A is the button and high is the condition of the button being pressed.

What will happen when the condition is false? When the condition is false, all of the sequence inside the loop will run.

What will happen when the condition is true.

If the condition is true, the actions will stop.

Well done If you got that one right.

In both the algorithm and the programme, a loop was stopped when a condition was met.

That is because they are using the do until structure.

When writing algorithms and programmes, you may want a set of actions to be carried out if the condition is met.

This is called selection.

The structure if, then is used when giving these commands.

Here is an example of selection.

If it is after seven o'clock, then get out of bed and get ready for school.

If is used to give the condition, which is after seven o'clock.

Then is used to give the actions to carry out when the condition is true.

This has been written with indentation to make it clear that the commands are linked.

So indentation is when you start your line a little bit further in from the line before.

You can see that there with the arrow.

This makes it clear that the commands are linked, the if and the then.

let's do a check here.

Match the word or phrase to the definition.

We have got the words algorithm, condition, and selection.

And we have the definitions.

A statement that can be either true or false.

Part of a programme where if a condition is met, then a set of commands is run.

And a precise sequence of steps that can be followed to do a task.

Pause the video here to match the words and the definitions.

Welcome back.

Let's look at the answers.

So algorithm matches with the definition a precise sequence of steps that can be followed to do a task.

Conditions are statements that can be either true or false, and selection is a part of the programme where if a condition is met, then a set of commands is run.

Well done if you got that all right.

What condition needs to be met for the person to put their umbrella up? Izzy says, "It's raining." How could you show this using selection? Pause the video and have a think about that.

This is how it can be written using selection.

If it is raining, then put up your umbrella.

The condition is the line starting with if, if it is raining.

The actions are the lines starting with then, then put up your umbrella.

Let's do a check here.

What could be the condition that may have been met for someone drinking a glass of water? Pause the video and have a think about that.

Welcome back.

What could be the condition that may have been met for someone drinking a glass of water? Being thirsty.

How could you show this with selection? Pause the video and have a think about your answer.

One answer is if you are thirsty, then have a glass of water.

Well done if you laid that out correctly.

Okay, it's time to get ready for a task now.

Laura has a new pet.

To keep the pet happy and healthy, she needs to look after it.

Write three if then selection statements to help Laura do this.

You can decide what kind of pet Laura has.

Laura's giving an example here.

If my pet is hungry, then I will feed it.

I'm sure you're thinking of all kinds of pets that Laura could have and some statements to help Laura look after them.

Remember that you need to write three statements.

Pause the video here to work on that, and I'll be here when you get back.

Welcome back.

So your examples for this task might be quite different to mine.

It will depend on what animal you chose.

I went with a dog.

So here were my three if then statements.

If my dog's water bowl is empty, then I will fill it up with fresh water.

If my dog is scratching at the door, then I will take it for a walk.

If my dog has dirty fur, then I will give it a bath.

Remember that your then part of the statement should be on the second line, and it should be indented so that means it goes off a bit more to the right to show that the two commands are linked.

Well done for your hard work on that task.

In the next part of the lesson, you are going to use loops to repeatedly check conditions.

A Crumble microcontroller can be used to control various inputs and outputs.

You can see in this diagram we have battery pack, a Crumble, Sparkle, and a push button switch.

In a circuit, a push button can be used as an input.

You can use selection with a button.

So in this example it reads, if the button is pressed, then your action will occur.

Let's do a check here.

What function will a push button have in a circuit? Is it A, output, B, input, C process, or D, storage? Pause the video to have a think about that.

Welcome back.

A push button will be an input in a circuit.

Well done if you got that right.

How do you think selection should be used to control a light pattern? Jacob says, "The light could change colour when the button is pressed." Sam says, "The button could make the light switch off and on." You might have thought of another idea too.

Well done if you did.

Here is an example of selection using a button in a Crumble circuit.

If the button is pressed, then make the Sparkle light red and wait for one second.

Selection is represented by this command block.

You can see we've got the if then and if block here with the hexagon space where you would put the condition.

This is how Crumble shows a button.

Press the button being pressed is the condition.

This is being shown by the command block A is HI.

If the button is pressed, then the condition is met.

The commands in the loop will run.

In this programme, the light pattern did not run when the push button was pressed.

Izzy says, "Why?" have a good look at this programme and see if you can figure it out.

When using selection in programmes, you must instruct the device to check constantly if the condition has been met.

To do this, you use repetition in the form of an infinite loop.

If you do not use repetition, the device will only check once if the condition has been met.

In programmes, you need to instruct the device to repeatedly check if the condition has been met.

Otherwise, it will only check once when the programme is run.

By adding an infinite loop, that's a forever loop, you can repeatedly check if the condition has been met.

Can you see how we've added the forever loop on the outside of our first loop? Laura says, "This way, the action will be carried out." This is because the forever loop will continuously be checking to see if the condition has been met.

Let's do a check here.

Why is an infinite loop needed when programming with conditions? Is it A, it will repeatedly check if the condition has been met, B, it will check if the condition has been met just once, or C, it will not check if the condition has been met? Pause the video here to think about your answer.

Welcome back.

The answer is A.

An infinite loop is needed because it will repeatedly check if the condition has been met.

Well done if you've got the answer correct.

Okay, it's time for the task now.

I want you to write an algorithm that uses selection to control a light pattern.

Remember to show that your algorithm is checking the condition repeatedly using an infinite loop.

Off you go to work on that part of the task.

And when you come back, I'll show you an example.

Welcome back.

Let's look at the algorithm that uses selection to control a light's pattern.

The algorithm reads, do the following continuously.

If the button is pressed, make the Sparkle light green.

Wait one second.

Turn the sparkle off.

Make the sparkle light red.

Wait one second.

Turn the Sparkle off.

Your algorithm might look different from this, and that is fine, but make sure it is using a way of showing the forever loop and a way of showing the conditional loop as well, the if then loop too.

In this part of the task, I want you to write a programme to adapt your algorithm into Crumble code.

You should use a do forever loop as well as a if then loop.

Off you go to work on your programming.

And when you come back, I can show you an example.

Welcome back.

Well done for all of your hard work during that task.

You can see here that we started with a forever loop.

And inside the forever loop, we use the conditional loop of if then and if.

Inside the if then conditional.

We use the A is Hi to set the condition, and then inside we set the sequence of changing the Sparkle light as well as waiting and turning the Sparkle off to make our light pattern.

Your programme will be different from this, and that's fine.

You have learned a lot today about using selection to control the flow of a programme.

Let's summarise our learning.

When writing algorithms and programmes, you may want a set of actions to be carried out if a condition is met.

This is called selection.

The structure if then is used when giving these commands.

To ensure that a programme repeatedly checks the condition, repetition is used.

You use an infinite loop in your code to see if the conditions have been met.

Then the actions will trigger.

Thank you for your hard work during this lesson, and I hope to see you again soon.