39
Programming: Simple Control Structures Alice

Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Programming:Simple Control Structures

Alice

Page 2: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Control Statements

We have been using Do in order andDo together to control the way instructionsare executed in your Alice program.Control statements can also be used for

conditional execution repetition

Page 3: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Conditional ExecutionConditional execution is where somecondition is checked and a decision is madeabout whether a block of the program will beexecuted.Conditional execution is extremely useful in

games simulations real-time controls, e.g. robot systems

Page 4: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

ExampleAs a simple example ofconditional execution, let'srevisit the Egyptian scene inthe Hollywood movie set.

The camera angle canmislead our perception of thescene We want to check a condition(is the mummy taller than thepharaoh?) and then performan action based on thewhether the condition is true.

Page 5: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

If/Else

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

Page 6: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

StoryboardIn our example, we can demonstrate which object is thetallest by having that object turn around.A storyboard design for this action is:

The condition in an If statement is a Boolean functionreturning a Boolean (true or false) value.

If mummy is taller than pharaoh mummy turns 1 revolutionElse pharaoh turns 1 revolution

Page 7: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Demo

Ch03Lec2mummyIfElseConcepts illustrated in this exampleprogram:

The condition of an If/Else statement isconstructed by using a function that returns aBoolean value (true or false). Do nothing in the Else part of the statementmeans that if the condition is false, Alice willskip this part and go to the next instruction.

Page 8: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

A different scenario

In some cases, the built-in functions are notsufficient for a condition that we want to check.

For example, the built-in function is taller thancompares the heights of two objects. We used thisfunction to compare the heights of the mummy andthe pharoah.Suppose, however, that the casting call for themummy requires that the actor is more than 2 meterstall. How can we compare the height of the mummyto a specific measurement (2 meters)?

Page 9: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Relational OperatorsIn situations where youneed to write your owncomparison, you can usea relational operator.Relational operators areprovided in the World'sbuilt-in functions.

Page 10: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Demo

Ch03Lec2mummyRelationalOpsConcepts illustrated in this example program:

Relational operations are defined using theworld'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 byeither a function or a number.

Page 11: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Example

Let's write code to make the mummy "walk" – asomewhat stilted motion like you would see in anold Hollywood horror film.The code will be more complex because weneed to

alternate left and right leg motions coordinate leg motions with the body moving forward

Page 12: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Storyboard

Do in order

Do together //move body and left leg mummy move forward 0.25 meters Do in order mummy's left leg move backward mummy's left leg move forward

Do together //move body and right leg mummy move forward 0.25 meters Do in order mummy's right leg move backward mummy's right leg move forward

Page 13: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Demo

Ch03Lec2mummySimpleSteps

Page 14: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Need for Repetition

In this example the mummy takes 1 stepforward on each leg.Suppose we want the mummy to take 20steps forward?

Page 15: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Loop

The Loop statement is a simple controlstructure that provides for repeating aninstruction (or block of instructions) acounted number of times.

Page 16: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Demo

Ch03Lec2mummyLoop

Page 17: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Classes, Objects, andWorld-level Methods

Alice

Page 18: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Larger ProgramsAs you become more skilled in writingprograms, you will find that programsquickly increase to many, many lines ofcode.Games and other "real world" softwareapplications can have thousands, evenmillions of lines of code.

Page 19: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Classes, Objects, & MethodsObject-oriented programming uses classes,objects, and methods as basic programmingcomponents.These components help to

organize a large program into small modules design and think about an intricate program find and remove errors (bugs)

Page 20: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

In our programs,we have been using…

Classes In Alice, classes are predefined as 3D models

Objects An object is an instance of a class.

Class: Frog (Uppercase name) Objects: frog, frog1, frog2, frog3 (lowercase names)

Page 21: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

We have also used…

built-in (predefined) methods Examples: move, turn to face, say

World.my first method Example:

In the Snowpeople world, we wrote program code where thesnowman tried to get the attention of a snowwoman.

All the program code was written in this one method, see nextslide…

Page 22: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy
Page 23: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Potential ProblemThe program code just seemed to growand grow.If we continue to write programs this waythe programs will become longer andmore difficult to read and think about.

Page 24: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Solution

A solution is to organize the instructionsinto smaller methods.A possible storyboard

Do in order

catchAttention – snowman tries to get the attention of the snowwoman

blink eyes – snowwoman turns to look and the snowman blinks his eyes

react – snowwoman blushes and turns away and the snowman

is disappointed

Page 25: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Next Step

The next step is to break down each majortask into simpler steps.

Example:

catchAttention

Do in order

snowman's head turns to face camera

snowman says "Ahem"

snowman's head turns to face snowwoman

Page 26: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Stepwise Refinement

The process of breaking a problem downinto large tasks and then breaking eachtask down into simpler steps is calledstepwise refinement.Once the storyboard is completed, wewrite a method for each task.

Page 27: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Demo: Starting a new method

First, to associate the newmethod with the World

• select the World tile in theObject Tree

•select the methods tab in thedetails area

•click on the "create newmethod" button

Page 28: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

DemoCh04Lec1SnowpeopleConcepts illustrated in this example world:

catchAttention is a world-level methodbecause it is defined as a method for Worldand has instructions that involve more thanone object (snowman, snowwoman, camera)The catchAttention method is executed bycalling (invoking) the method .

Page 29: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Why?Why do we want to write our own methods?

saves time -- we can call the method again and again withoutreconstructing code reduces code size – we call the method rather than writing theinstructions again and again allows us to "think at a higher level"

can think catchAttention instead of“turn head to face the camera, then say ‘Ahem’ while moving eyes up and

down" the technical term for "think at a higher level" is "abstraction"

Page 30: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Parameters

Alice

Page 31: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

A beetle band

Our task is to create an animation for a bug band as anadvertisement for their next concert.

Page 32: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Storyboards

Each bug band member will perform a solo.

Do together Do in order georgeBeetle move up georgeBeetle move down play sound

Do together Do in order ringoBeetle move up ringoBeetle move down play sound

Do together Do in order paulBeetle move up paulBeetle move down play sound

Do together Do in order lennonBeetle move up lennonBeetle move down play sound

Page 33: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

DemoCh04Lec2BeetleBand-v1

Concepts illustrated To play a sound, a sound file must first be importedinto Alice. (Alice is not a sound editor.) This code is only for georgeBeetle.

Three more methods (one for each band member)will be needed!

Page 34: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

A Better Solution

Four versions of very similar code seemsa bit tedious. The only things that changeare the beetle and the music that plays.A better solution is to write a more flexiblemethod.

Page 35: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Parameters

Built-in methods provide flexibility by providingparameters such as distance and direction.Parameters allow you to pass in values(arguments).

Example

Parameters: distance, direction Arguments: 0.5 meters, 0.5 seconds

Page 36: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

Kinds of Parameters

Alice provides several kinds of parameters thatcan be used in your own methods.

Page 37: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

The storyboard

solo

Parameters: bandMember, musicDo together Do in order bandMember move up bandMember move down play music

In this example, we can write just one method and use parameters to specify:

which band member is to perform and which music should be played.

Page 38: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

DemoCh04Lec2BeetleBand-v2Concepts illustrated

Enter name and select the type of each parameter bandMember is an Object parameter music is a Sound parameter

A parameter acts as a placeholder in the instruction Arguments are passed to the parameter in the callto the method

Page 39: Programming: Simple Control Structures...mummy's right leg move backward mummy's right leg move forward Demo Ch03Lec2mummySimpleSteps Need for Repetition In this example the mummy

A Number parameterAdd a Number parameter to specify the height thebandMember jumps up and down.

Note that the call to the method must now include anargument for the height.