1 / 16

Programming: Simple Control Structures

Programming: Simple Control Structures. MMP 220 Multimedia Programming.

novia
Download Presentation

Programming: Simple Control Structures

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. Programming: Simple Control Structures MMP 220 Multimedia Programming The material in this handout is taken from “Learning to Program with Alice” a project supported by the National Science Foundation under Grant NSF-0126833, NSF-0302542, and NSF-0339734. Contact Wanda Dann: wpdann@ithaca.edu, Stephen Cooper: scooper@sju.edu, Randy Pausch: pausch@cmu.edu, and Don Slater: dslater@cmu.edu http://www.aliceprogramming.net/ This adapted material was prepared for students in MMP220 as as part of a curriculum redesign project funded by National Science Foundation grant #0511209 Co PI’s Christopher Stein and Jody Culkin BMCC CUNY http://teachingmultimedia.net

  2. Control Statements • We have been using Do in order and Do together to control the way instructions are executed in your Alice program. • Control statements can also be used for • conditional execution • repetition

  3. Conditional Execution • Conditional execution is where some condition is checked and a decision is made about whether a block of the program will be executed. • Conditional execution is extremely useful in • games • simulations • real-time controls, e.g. robot systems

  4. Example • As a simple example of conditional execution, let's revisit the FirstEncounter world. • As the robot moves closer, the alien hides behind the rocks. • We want to check a condition (is robot to short to over the rock?) and then perform an action based on the whether the condition is true.

  5. If/Else In Alice, an If/Else control statement is used to check a condition and make a decision.

  6. Storyboard • A storyboard design for this conditional statement is: • The condition in an If statement is a Boolean function that yields a true or false value. If spiderRobot is shorter than rock Do in order spiderRobot neck move up spiderRobot neck move down Else Do nothing

  7. Demo • Ch03Lec2FirstEncounterIfElse • Concepts illustrated in this example program: • The condition of an If/Else statement is constructed by using a function that returns a Boolean value (true or false). • Do nothing in the Else part of the statement means that if the condition is false, Alice will skip this part and go to the next instruction.

  8. A different scenario • In some cases, the built-in functions are not sufficient for a condition that we want to check. • For example, the built-in function is shorter than compares the heights of two objects. • Suppose, however, that we wanted to compare the robot's height to 2 meters. • How can we compare the height of the robot to a specific measurement (2 meters)?

  9. Relational Operators • In situations where you need to write your own comparison, you can use a relational operator. • Relational operators are provided in the World's built-in functions.

  10. Demo • Ch03Lec2FirstEncounterRelationalOps • Concepts illustrated in this example program: • Relational operations are defined using the world's built-in functions. • Placeholder values are first selected for the "a" and "b" components of the relational expression. Then the placeholder values are each replaced by either a function or a number.

  11. Need for Repetition The walking action is not realistic: the robot moves forward to the rock (a distance of several meters) but the legs are simulating a walking action only once.

  12. One meter, one step • A more realistic action is to move the robot forward one meter for each step made by the legs. • But, now the robot is moving forward only 1 meter. This block of code will have to be repeated again and again to animate the robot moving close to the rock.

  13. Loop The Loop statement is a simple control structure that provides for repeating an instruction (or block of instructions) a counted number of times.

  14. Demo Ch03Lec2FirstEncounterLoop

  15. Demo • Ch03Lec1FirstEncounterExpressionV2 • Subtracting 2 meters from the distance is an arbitrary amount. • To be more precise, we could subtract the width of the rock. • The resulting expression subtracts the value of one function from the value of another function.

  16. Assignment • Read Chapter 3

More Related