Programming:Simple Control Structures
Alice
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
 
Conditional Execution
Conditional 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
Example
As a simple example ofconditional execution,let's revisit theFirstEncounter world.
 As the robot moves closer,the alien hides behind therocks.
 We want to check acondition (is robot to shortto over the rock?) and thenperform an action based onthe whether the condition istrue.
3-2-3
If/Else
In Alice, an If/Else control statement is used tocheck a condition and make a decision.
3-2-1
Storyboard
A storyboard design for this conditionalstatement is:
 
The condition in an If statement is a Booleanfunction 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
Demo
Ch03Lec2FirstEncounterIfElse
Concepts 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.
Another Example
As another 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.
3-2-2
Storyboard
In 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 revolution
Else
       pharaoh turns 1 revolution
Demo
Ch03Lec2mummyIfElse
A different scenario
In some cases, the built-in functions arenot sufficient for a condition that we wantto check.
 For example, the built-in function is shorterthan compares the heights of two objects.
 Suppose, however, that we wanted tocompare the robot's height to 2 meters.
 How can we compare the height of the robotto a specific measurement (2 meters)?
Relational Operators
In situations where youneed to write your owncomparison, you can usea relational operator.
Relational operators areprovided in the World'sbuilt-in functions.
Demo
Ch03Lec2FirstEncounterRelationalOps
Concepts 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.
Another scenario
Let’s compare the height of the mummy to a specificmeasurement (2 meters).
Ch03Lec2mummyRelationalOps
Need for Repetition
The walking action is not realistic: the robotmoves forward to the rock ( a distance ofseveral meters) but the legs are simulating awalking action only once.
3-2-7
One meter, one step
A more realistic action is to move the robot forwardone 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 andagain to animate the robot moving close to the rock.
3-2-8
Loop
The Loop statement is a simple controlstructure that provides for repeating aninstruction (or block of instructions) acounted number of times.
Demo
Ch03Lec2FirstEncounterLoop
Another 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
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
 
Demo
Ch03Lec2mummySimpleSteps
Need for Repetition
In this example the mummy takes 1 stepforward on each leg.
Suppose we want the mummy to take 20steps forward?
Demo
Ch03Lec2mummyLoop
Assignment
Read Chapter 3 Section 2
 Simple Control Structures
Read Tips & Techniques 3
 Engineering Look & Feel
     (Texture Maps, Fog)
Lab
Chapter 3 Lab Lec2