1 / 15

Execution Control with If/Else and Boolean Questions Part 2

Execution Control with If/Else and Boolean Questions Part 2. Alice. The Zeus world revisited. Recall the Zeus world Testing this world, we found two significant problems Anything the user clicked got zapped by Zeus’ bolt – not just philosophers!

Lucy
Download Presentation

Execution Control with If/Else and Boolean Questions Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Execution Control with If/Else and Boolean QuestionsPart 2 Alice

  2. The Zeus world revisited • Recall the Zeus world • Testing this world, we found two significant problems • Anything the user clicked got zapped by Zeus’ bolt – not just philosophers! • Philosophers could get zapped with lightning more than once!

  3. Problem • What we need in the Zeus world is conditional execution • Check conditions: • Is the selected object a philosopher? • If so, has the philosopher already been zapped by lightning? • Conditional execution: • lightning bolt will be shot or not

  4. Multiple Conditions • First, we’ll tackle the problem of restricting Zeus only to shoot thunderbolts at philosophers. • This is different from previous examples in that four possible conditions must be checked – the user could click on any one of the four philosophers.

  5. One condition • Let's begin with just one philosopher – homer. drag in the who tile, then select == and homer Start with a blank if statement… = = is a relational operator meaning “is equal to”

  6. The If statement encloses the Do in order block. (We say the Do in order is nestedinside the If.) Now, the lightning will strike homer only if the user clicks on homer. But, what about the other three philosophers?

  7. Logical operators Use Boolean logic operators to check more than one condition.

  8. Demo • A demonstration of the Zeus world with If statement and logic operators.

  9. Abstraction • Multiple conditions, as in this example, • become complex • are difficult to read • are difficult to debug • A better solution is to write a Boolean question that checks the conditions. • This is an example of abstraction – allowing you to think on a higher plane.

  10. We used nested If statements to check each possible condition. • If one of the conditions is found to be true, the question immediately returns true and the question is all over! • If none of the conditions are true, the question returns false.

  11. Demo • Demonstration of the Zeus world with the isPhilosopher question.

  12. Completing the Zeus world • The second problem is how to prevent lightning striking the same philosopher more than once. • How do we know if a philosopher has already been struck by lightning? • When a lightning bolt strikes, the philosopher “is zapped” and the his color property is set to black. • Checking color is a convenient way to check for a previous lightning strike.

  13. Preventing a duplicate strike • We only need to check for a possible duplicate-strike if we already know that a philosopher is clicked • the way to handle this is nest a second If statement inside the first. != is a relational operator meaning “is not equal to”

  14. Demo • A demonstration of completed Zeus world

  15. Assignment • Read Chapter 6-2, Questions and If/Else • Read Tips & Techniques 6, Random Numbers & Random Motion • Lab 6-2

More Related