Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS320n –Visual Programming

Similar presentations


Presentation on theme: "CS320n –Visual Programming"— Presentation transcript:

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 Decision Making in Programs
What We Will Do Today Work more with if / else statements using the Zeus lighting bolt world Visual Programming Decision Making in Programs

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 Visual Programming Decision Making in Programs

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” Visual Programming Decision Making in Programs

5 Decision Making in Programs
Logical Operators It is possible to check more than one condition in a boolean statement Visual Programming Decision Making in Programs

6 Decision Making in Programs
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 Visual Programming Decision Making in Programs

7 Checking Multiple Conditions
drag boolean logic, either or to replace condition on if set both values to true initially Visual Programming Decision Making in Programs

8 Decision Making in Programs
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 Visual Programming Decision Making in Programs

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 Visual Programming Decision Making in Programs

10 Checking All Four Philosophers
long, but does check all 4 philosophers DEMO Visual Programming Decision Making in Programs

11 Decision Making in Programs
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 Visual Programming Decision Making in Programs

12 isPhilosopher function
isPhilosopher is still complex but this makes shootBolt look a lot simpler ………… Visual Programming Decision Making in Programs

13 Decision Making in Programs
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? Visual Programming Decision Making in Programs

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. Visual Programming Decision Making in Programs

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” Visual Programming Decision Making in Programs

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 who’s color is not black” Visual Programming Decision Making in Programs

17 Decision Making in Programs
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!” Visual Programming Decision Making in Programs


Download ppt "CS320n –Visual Programming"

Similar presentations


Ads by Google