Adding an Upper Limit to the Times to Dance

Adding an Upper Limit to the Times to Dance

Now, let's say that the Cat will not only refuse to dance 0 or for a negative amount of times but that it will also refuse to dance more than 100 times - because if it dances this much it will get worn out!

To add this upper limit we are going to have to use Nested Conditionals, which is also sometimes referred to Nested Branching.

Nested Conditionals in Scratch involves having if then and/or if then else blocks inside of other if then and/or if then else blocks. This is similar to the Nested Loops we looked at earlier, where we have repeat blocks inside other repeat blocks.

We want to change our Dancing Cat program so that the following happens:

  • The Cat asks how many times you want it to dance
  • We check if the user answers with 0 or a negative number
    • If the answer is 0 or a negative number, the Cat tells the user it can't dance that many times and the program ends
    • Otherwise (if the answer is positive and not 0), we then check if the answer is larger than 100
      • If the answer is larger than 100, the Cat tells the user it can't dance that many times and the program ends
      • Otherwise, the answer is not larger than 100, the Cat then dances for the number of times the user answered with and then the program ends

To turn this algorithm into program code we need to add another if then else block, as shown in the blocks below:

Have a look at the bullet points above that describe the algorithm, the Scratch blocks and compare the two. Can you see how these are related to each other, and how the different instructions will be followed based on the user input?


Views
1 Total Views
1 Members Views
0 Public Views
Share on Social Networks
Share Link
Share by mail

Please login to share this webpage by email.