CS320n Visual Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CS320n Visual Programming

Description:

Anything the user clicked got zapped by Zeus' bolt not just philosophers! ... A demonstration of the Zeus world with If statement and logic operators. ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 18
Provided by: sco1
Category:

less

Transcript and Presenter's Notes

Title: CS320n Visual Programming


1
CS320n Visual Programming
  • Execution Control with If / Else and Boolean
    Functions
  • (Slides 6-2-2)

Thanks to Wanda Dann, Steve Cooper, and Susan
Rodger for slide ideas.
2
What We Will Do Today
  • Work more with if / else statements using the
    Zeus lighting bolt world

3
The Zeus world revisited
  • Recall the Zeus world
  • Testing this world, we found three significant
    problems
  • Anything the user clicked got zapped by Zeus
    bolt not just philosophers!
  • Philosophers could get zapped with lightning more
    than once!
  • if we clicked on Zeus the program halted

4
Checking One Philosopher
  • Check to see if who is homer
  • add an if / else to the shootBolt method
  • Drag in the who tile then select and homer
  • is a relational operator meaning is equal to

5
Logical Operators
  • It is possible to check more than one condition
    in a boolean statement

6
Philosophers Only
  • We only want to shoot the bolt when one of the 4
    philosopher's is clicked
  • could make 4 separate events like in the fire
    truck saving people from the burning building
  • OR could check in the shootBolt method if
    parameter is one of the philosophers
  • if who is a philosopher shoot them
  • else do nothing

7
Checking Multiple Conditions
  • drag boolean logic, either or to replace
    condition on if
  • set both values to true initially

8
changing condition
  • right now the condition is always true
  • change it so it checks if the parameter who is
    equal to Homer or Plato.
  • Drag who into the condition

9
Adding More Philosophers
  • If who is equal to plato or homer we will shoot
    them
  • add in the other philosophers by nesting logical
    operators
  • Replace trues with a check for who being equal to
    the other three philosophers

10
Checking All Four Philosophers
  • long, but does check all 4 philosophers
  • DEMO

11
Complexity
  • The boolean condition in the if statement that
    checks to see if who is a philosopher is rather
    complex
  • someone reading the code may not understand
    exactly what the purpose of that condition is
  • create a function to check if who is a
    philosopher
  • make complex things appear simple abstraction

12
isPhilosopher function
  • isPhilosopher is still complex but this makes
    shootBolt look a lot simpler


13
Demo
  • A demonstration of the Zeus world with If
    statement and logic operators.
  • What problem have we fixed?
  • If we want to add another person to the scene
    that Zeus can shoot what changes must be made to
    the program?
  • What problem still exists?

14
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.

15
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
16
Alternative to Nested if
  • Instead of using a nested if we could the logical
    expression a and b as the test for the if
  • a is the parameter who is a philospher
  • b is the parameter whos color is not black

17
Demo
  • A demonstration of completed Zeus world
  • Does it matter if use nested if or a more complex
    boolean condition?
  • May be easier to use nesting to achieve certain
    behaviors
  • example, if a philosopher has already been shot,
    he says Leave me alone!
Write a Comment
User Comments (0)
About PowerShow.com