1 / 13

Execution Control with If/Else and Boolean Functions

Execution Control with If/Else and Boolean Functions. Alice. Adding More Complexity to Worlds. Initially programs were not interactive program ran and user watched Then learned how to make programs interactive add events to program when event occurs an event handler method is called

kevin-blake
Download Presentation

Execution Control with If/Else and Boolean Functions

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 Functions Alice

  2. Adding More Complexity to Worlds • Initially programs were not interactive • program ran and user watched • Then learned how to make programs interactive • add events to program • when event occurs an event handler method is called • adds variability to programs • not always the same thing happening

  3. Event Driven Programming • a whole philosophy of programming called “event driven” programming • flow of program controlled by external events as opposed to things internal to the program • very important in user interfaces • the parts of programs that interact with real users • think about using a computer • the operating system ( Windows XP, Mac OS X, etc. ) has a large component that is event driven • responding to your actions. (Move mouse, click, type)

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

  5. Examples of Decisions • A race car simulation • driver provides input from steering wheel, gas pedal, and brake • if car stays on road display next section of road • if car hits another car, crash • if car goes too far off road, crash • if pass checkpoints, more time

  6. Logical Expressions • Program must make decision based on current conditions • on road? • time left? • intersected another car?

  7. Logical Expressions • A condition is checked in a logical expressionthat evaluates to Boolean value • true or false (Boolean) value • car on road true • car over finish line false

  8. 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 • functions • methods

  9. Example: Boolean Function • Suppose you are building a simulation system used to train air traffic controllers. • One of the tasks of an traffic controller is to be alert for possible collisions in the flight space.

  10. 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 function that checks the vertical distance against a minimum difference in altitudes. • The function 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

  11. Demo • Lab12FlightCollision-V1 • Concepts illustrated in this example • A world-level relational operator is used to create a Boolean expression as the condition. • The absolute value function is used to make sure the computed difference in altitude is not a negative number.

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

  13. Demo • Lab12FlightCollision-V2 • Concepts illustrated in this example • Decisions were made to • control whether a method is called • determine which set of instructions are immediately executed

More Related