Year 5 - Lesson 3: Nesting Ifs
Learning Intention
I can use nested if-then-else instructions in my program.
I can use nested if-then-else instructions in my program.
Introduction
In the last lesson we created quiz programs that used conditionals. Each time the program runs the same questions are always asked; Question 1, and then Question 2, then Question 3.
In the last lesson we created quiz programs that used conditionals. Each time the program runs the same questions are always asked; Question 1, and then Question 2, then Question 3.
It can be useful to have a program where the next question Picoh asks depends on how the previous one has been answered. You might be familiar with this when you ask a personal assistant to set an alarm. Only if you ask it to set an alarm will the question ‘What time would you like me to set the alarm for?’ be asked. Or perhaps you have played a maths game that has become more difficult as you get more answers right. The programs select the next question based on how the previous one was answered.
For us to do this we can nest If-then-else conditional instructions like this:
For us to do this we can nest If-then-else conditional instructions like this:
Ask students which conditions need to be set true or false to run each of the action blocks Do1 to Do7.
Let’s look at how this can be used to make the second quiz question more difficult if the first answer is answered correctly, and easier if it is not. This is the code from the previous lesson.
Let’s look at how this can be used to make the second quiz question more difficult if the first answer is answered correctly, and easier if it is not. This is the code from the previous lesson.
Now here's a trickier follow up question:
Can you work out where in the code the sequence would need to go so that the trickier question is only asked if the answer to the first question is right?
And how about this code for a question that is easier than the first one.
And how about this code for a question that is easier than the first one.
Can you work out where this could be put into the code so that it is only asked if the answer to the first question is wrong?
We can nest further of course with the answer to the second question determining the third question that is asked. Before long things can get a bit overloaded as the code becomes enormous.
Activity 1:
Can you program Picoh to run a quiz that always asks at least three-questions with more difficult questions if the user gets answers right and easier questions if the user gets answers wrong?
Extension:
Can you add a score based on the number of questions that are answered correctly?
You could try creating a variable, setting it to zero at the start and increasing it each time a correct answer is given
Plenary:
Nesting can become very unwieldy when you go beyond several layers deep. How many if-then-else
conditionals do you think would be needed to create a quiz that always asked at least 6 questions?
Is there another way of organising a program?
We can nest further of course with the answer to the second question determining the third question that is asked. Before long things can get a bit overloaded as the code becomes enormous.
Activity 1:
Can you program Picoh to run a quiz that always asks at least three-questions with more difficult questions if the user gets answers right and easier questions if the user gets answers wrong?
Extension:
Can you add a score based on the number of questions that are answered correctly?
You could try creating a variable, setting it to zero at the start and increasing it each time a correct answer is given
Plenary:
Nesting can become very unwieldy when you go beyond several layers deep. How many if-then-else
conditionals do you think would be needed to create a quiz that always asked at least 6 questions?
Is there another way of organising a program?