Presentation is loading. Please wait.

Presentation is loading. Please wait.

Execution Control with If/Else and Boolean Questions Part 1 Alice.

Similar presentations


Presentation on theme: "Execution Control with If/Else and Boolean Questions Part 1 Alice."— Presentation transcript:

1 Execution Control with If/Else and Boolean Questions Part 1 Alice

2 Thinking About More Advanced worlds No doubt you have started to think about building animations like simulations and video games… To build more advanced worlds, you will need to write code that involves decisions

3 Examples of Decisions In a car-race simulation, the driver steers the car around curves and past mile- markers. If the car stays on the road, the score increases. If the car goes off the road into the stands, the car crashes. If the driver gets the car over the finish- line, the time is posted and the driver wins!

4 Logical Expressions A decision is made based on current conditions A condition is checked in a logical expression that evaluates to true or false (Boolean) value car on road true car over finish line false

5 If/Else In Alice, a logical expression is used as the condition in an If/Else control structure. Decisions (using If/Else) are used in questions methods

6 Example: Boolean Question Suppose you are building a simulation system used to train flight controllers. One of the tasks of a flight controller is to be alert for possible collisions in the flight space.

7 Storyboard One factor in determining whether two aircraft are in danger of collision is the vertical distance (difference in altitudes) between them. We can write a question that checks the vertical distance against a minimum difference in altitudes. The question returns true if they are too close, otherwise false. isTooClose: Parameters: aircraftOne, aircraftTwo, minimumDistance If the vertical distance between aircraftOne and aircraftTwo is less than minimumDistance return true Else return false

8 Vertical distance To find the difference in altitude, we use the built-in distance above question. We don't know which aircraft is above the other. To avoid a possible negative value, we take the absolute value of the distance. Note: The nesting of tiles acts like parentheses in math expressions.

9 Using a relational operator Use the < relational operator from the World's built-in questions to check the vertical distance against a minimum.

10 Calling the question The Boolean question, isTooClose is called as the condition for avoiding a collision.

11 Storyboard To avoid a collision, the aircraft that is above the other should move up and the lower aircraft should move down. avoidCollision: Parameters: aircraftOne, aircraftTwo If aircraftOne is above aircraftTwo Do together aircraftOne move up aircraftTwo move down Else Do together aircraftOne move down aircraftTwo move up

12 Demo Demonstration of the fly space collision simulation. Note: This example illustrates three decisions one in a Boolean question one that controls whether a method is called one that determines which set of instructions are immediately executed


Download ppt "Execution Control with If/Else and Boolean Questions Part 1 Alice."

Similar presentations


Ads by Google