Year 5 - Lesson 2
(If-then)
Learning Intention
- I can use the if-then instruction.
Introduction
What did we learn to do last week? (use the current operator) This week we will start with another task which uses current but we will combine this with another new piece of code, the if-then instruction. What does if mean? Can you put if into a sentence? Collect the best ideas on a class working wall. Where can we find the if-then instruction? (Control palette). What does the if-then instruction look like? if-then is a conditional instruction, this means the condition has to go in the empty hexagon and the result goes in the middle of the two arms.
What did we learn to do last week? (use the current operator) This week we will start with another task which uses current but we will combine this with another new piece of code, the if-then instruction. What does if mean? Can you put if into a sentence? Collect the best ideas on a class working wall. Where can we find the if-then instruction? (Control palette). What does the if-then instruction look like? if-then is a conditional instruction, this means the condition has to go in the empty hexagon and the result goes in the middle of the two arms.
Teacher Input 1
Ohbot can say the correct response for the time of day. If it is before 12 noon Ohbot can say good morning and if it is after 12 noon Ohbot can say good afternoon. We will now work together to write the code for these two if instructions. We need to start our program with a when green flag clicked. We will then need two if instructions - one for if it is before 12 noon and one for if it is after 12 noon.
Ohbot can say the correct response for the time of day. If it is before 12 noon Ohbot can say good morning and if it is after 12 noon Ohbot can say good afternoon. We will now work together to write the code for these two if instructions. We need to start our program with a when green flag clicked. We will then need two if instructions - one for if it is before 12 noon and one for if it is after 12 noon.
We need to go to the Operators palette to find a hexagonal shaped operator to help us build the condition. In this example we need a greater than (>) operator and a less than (<) operator. If the hour is less than 12 the result is that we say ‘good morning’ and if the hour is greater than 12 the result is we say ‘good afternoon’. File, Save As – ‘if hour’.
We can take this further because Ohbot can ask you a question. You can type an answer and then in turn Ohbot can respond to your answer. For example, Ohbot can ask you your name and respond to the answer you type.
To ask a question we need to go to the Sensing palette and use an ask and wait instruction. Ohbot can reply by saying hello and using the response you typed in. You need a join operator from the Operators palette and an answer variable from the Sensing palette. File, Save As – ‘ask name’.
We can take this even further by using the if-then instruction. Ohbot can respond to your question with different answers depending on your response to the question. For example, Ohbot can ask the question ‘What is your favourite vegetable?’ Ohbot can respond appropriately depending on the answer you typed.
Activities
1. Can you write some code to ask the question, 'What is your favourite vegetable?' and respond appropriately?
2. Can you write your own question for Ohbot to ask and respond to?
1. Can you write some code to ask the question, 'What is your favourite vegetable?' and respond appropriately?
2. Can you write your own question for Ohbot to ask and respond to?
Extension 1
You will need a dice. Can you write a program to make Ohbot ask the score when you throw it, and respond appropriately to each possible score? Top tip - How many possible outcomes can you have with a dice? (6) Therefore, how many if instructions will you need?
You will need a dice. Can you write a program to make Ohbot ask the score when you throw it, and respond appropriately to each possible score? Top tip - How many possible outcomes can you have with a dice? (6) Therefore, how many if instructions will you need?
Extension 2
Can you edit your code?
Can you make Ohbot randomly generate an dice score between 1 and 6?
Can you edit your code?
Can you make Ohbot randomly generate an dice score between 1 and 6?
Plenary
Which tasks were you successful at? Show examples of children’s coding. Which activity did you enjoy the most?
Which tasks were you successful at? Show examples of children’s coding. Which activity did you enjoy the most?