Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.

Similar presentations


Presentation on theme: "CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for."— Presentation transcript:

1 CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

2 Elements of Visual Programming if / else statements2 What We Will Do Today Learn about –using if / else statements to control the flow of a program –using boolean functions

3 Elements of Visual Programming if / else statements3 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

4 Elements of Visual Programming if / else statements4 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)

5 Elements of Visual Programming if / else statements5 Internal Decision Making To build more complex programs and animations external decision making is needed in the form of events … But also need internal decision making important in many types of programs –good examples are simulations and games

6 Elements of Visual Programming if / else statements6 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

7 Elements of Visual Programming if / else statements7 Logical Expressions Program must make decision based on current conditions –on road? time left? intersected another car? Conditions are checked in a logical expression (also called a boolean expression) the logical expression evaluates to true or false –car on road -> true –car passed finish line -> false –car intersected any other cars -> false

8 Elements of Visual Programming if / else statements8 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 IF ELSE

9 Elements of Visual Programming if / else statements9 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.

10 Elements of Visual Programming if / else statements10 Problem Description two aircraft –biplane and helicopter as biplane moves towards helicopter make sure they do not collide if they are too close, they need to adjust altitude (height) or course (direction of travel)

11 Elements of Visual Programming if / else statements11 Problem Description two factors in determining whether two aircraft are in danger of collision –total distance between them center point to center point –vertical distance between them differences in height both distances must be greater than some safe distance otherwise aircraft are too close red line: total distance green line: vertical distance a method or a function? class level or world level?

12 Elements of Visual Programming if / else statements12 tooCloseByTotalDistance isTooCloseByTotalDistance: Parameters: aircraft1, aircraft2, minDistance return true if the distance between aircraft1 and aircraft2 is less than minDistance else return false Whether the distance between aircraft1 and aircraft2 is less than minDistance is an example of a boolean expression. It is either true of false return result of expression

13 Elements of Visual Programming if / else statements13 Steps in building function Select the world object and click on the functions tab select new function

14 Elements of Visual Programming if / else statements14 Steps in building function type in name of function pick Boolean for the return type (function will return true or false) add parameters for 2 objects (the aircraft) and a number (min distance)

15 Elements of Visual Programming if / else statements15 Steps in building function function returns true want to return if distance between aircraft is less than minDistance select World functions pick a < b and drag to replace true –example of a relational operator

16 Elements of Visual Programming if / else statements16 Steps in building function replace true fill in a and b with dummy values (like 1)

17 Elements of Visual Programming if / else statements17 Steps in building function would like to drag aircraft1 parameter and call distance to function can’t  select biplane and the distance to function drag that to replace 1

18 Elements of Visual Programming if / else statements18 Steps in building function replace dummy objects with parameters aircraft1 and aircraft2 replace second 1 with minDistance

19 Elements of Visual Programming if / else statements19 Completed Method

20 Elements of Visual Programming if / else statements20 Using if / else in World.my first method check to see if the aircraft are too close IF they are then avoid a collision –hard code motion OR –create a method to avoid the collision to use if / else in program drag if /else from bottom

21 Elements of Visual Programming if / else statements21 Using if / else if statement tests a boolean expression can be a relation operator OR a function that returns a boolean OR a boolean value (true / false)

22 Elements of Visual Programming if / else statements22 Completing the if / else replace parameters with biplane and helicopter call method avoid collision if the condition is true nothing to do if they are not to close

23 Elements of Visual Programming if / else statements23 Completing avoid collision assume if aircraft are too close one moves up and one moves down to increase the vertical distance between them –elevation separation does it matter which moves up and which moves down?

24 Elements of Visual Programming if / else statements24 using if / else is avoid collision Move the aircraft that is above the other up

25 Elements of Visual Programming if / else statements25 isTooCloseVerticalDistance check to see if aircraft are in the same strata to find difference in altitude use the built-in distance above function –don’t know which aircraft is above the other –to avoid possible negative value, use the absolute value of the distance (World level function)

26 Elements of Visual Programming if / else statements26 Adding Motion create a method that moves an aircraft forward some distance if the two aircraft are closer than twice the distance of the move –avoid collision if they are the vertical distance between them is too small –move aircraft forward twice the distance

27 Elements of Visual Programming if / else statements27 forward And Check Collision

28 Elements of Visual Programming if / else statements28 check For Height Collision


Download ppt "CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for."

Similar presentations


Ads by Google