Learning Intention

  • I can nest conditionals inside one another

Introduction

A quiz that always asks the same questions

In the last lesson we created quiz programs that used conditionals. Each time the program runs the same questions are always asked: Question 1, 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 becomes more difficult as you get more answers right.

Teacher Input

Diagram of nested if-then-else conditionals

For us to do this we can nest `if-then-else` conditional instructions. Ask students which conditions need to be set true or false to run each of the action blocks Do1 to Do7.

The quiz 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.

A trickier follow up question

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?

An easier follow up question

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.

Activities

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.

Questions:

  • 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?
Ohbot image

Key Points

  • Conditionals can be nested inside one another to branch a program.
  • Nesting lets the next question depend on how the previous one was answered.
  • Each level of nesting doubles the number of possible paths.
  • Deeply nested code quickly becomes hard to read — a reason to look for another structure.
  • A variable can hold a running score across the whole quiz.

Need Help?

If you're stuck on any part of this lesson, check out these resources:

Ohbot