Flow control determines the sequence of statements executed when a
program runs
which statements are executed and in what order
In movie-style programs (Alice text chapters 1-4) the sequence of actions
is completely determined by the programmer
every execution of a given program is the same
does not depend on any runtime data or events
Program input
In most programs the flow of control is determined by runtime input
The programmer determines how the program responds to input, but the
input is not determined until the program runs
There may be countless variations on how a given program behaves
this makes testing programs much harder!
Two kinds of input
Data input is read by the program when it wants it
we'll do lots of this using Python
User event input is generated when the program runs and the program
responds (almost) immediately
this is called interactive programming
Alice makes this easy (within its framework)
Two kinds of animation
Movie animations are not interactive at all
no input of any kind
every movie run is the same
Animated games are highly interactive
game events trigger short animated sequences
usually there is no action at all, or only something simple and
repetitive, unless there has been a recent event
no two game runs are the same
Alice events
Alice programs can respond to a variety of user generated events:
clicking the mouse
pressing a key on the keyboard
moving the mouse
starting the program
In response to events, Alice may
call a method
move an object
move the camera or change its orientation
We'll use the bold blue features above
Event handling methods
A method that is called in response to an event is called an event handler
When an event is linked to a method that performs an action, a program
behavior is created
You can change the method invoked by the Whentheworldstarts event so
it is no longer World.myfirstmethod
if World.myfirstmethod is not called at any other time, you need to
deleted it (drag to trash bin), or you will get a warning message when
the program starts indicating that the method cannot be reached
Which of the following cannot cause an event in Alice?
mouse click
keyboard key press
network activity
mouse motion
none (Alice can respond to any of the above)
Answer C
Which of the following is not true of functions
functions return values
functions can take arguments
functions can be used as instructions
functions in Alice are associated with objects
Answer: C
The most important consideration in program style is usually
Parameters allow one event handling method to handle four different events
Use multiple light sources for more precise control of lighting
in the demo the world light is turned off (0 brightness)
a lightBulb object provides light for the control panel
the pointLight range property appears to have no effect
Testing interactive programs
When parameters are used in interactive programming, it is especially
important to test that all possible parameter values work as expected
If event actions have significant interaction with one another, it is
necessary to test many possible permutations (combinations and orders) of
events in order to catch interaction bugs
there are not significant interactions in the last demo
There may be so many permutations that it is impossible to test them all,
in which case you have to be especially careful to program with the best
possible correctness and style
Functions in Alice
Demo: throwing a ball
Scenario: animate the motion of a ball thrown in the air
doing this with any physical accuracy requires calculating the effect
of gravity
the ball motion program in the Alice text section 6.1 does not do this
Physics: if the initial velocity is v, the velocity at time t is
v + at
where a is the acceleration
the acceleration due to gravity is -9.8 meters per second squared
A loop may have a index variable, the value of which changes each time
the body of the loop is executed
in Alice, click the show complicated version loop statement button to
expose this variable
an index variable starts with an initial value and is incremented by
some amount until it get up to (but in Alice not including) a given
final value
in our example, the variable index takes on the value 0, .02, .04,...,.98
How a function works
A function receives value(s), performs some computation on the value(s),
and returns (sends back) a value
Types of functions
The type of a function is the type of value it returns
a number
a specific object
a color
a Boolean (true or false)
other property values...
How function are like methods
Like methods, functions may be associated with any class, including World
so like methods, functions may be exported with their class, and the
same limitations apply on references to methods and functions outside
of the exported class
no references to other objects
no references to world-level functions you have written (built-in
world-level functions are fine to use)
Like methods, functions may have any number of parameters
How functions differ from methods
The body of a function contains only optional comment lines followed by a
return statement
the body of a method contains any number of statements
the return statement contains an expression, whose value is returned
when the function is called
in Alice, return statements are not allowed in methods
A function call is an expression, whereas a method call is a statement
In Alice, method calls may have parameters
in event handlers but not method bodies
in event handlers and method bodies
in only in method bodies
nowhere
Answer: B
In Alice return types are associated with
functions
methods
functions and methods
niether functions or methods, only variables
Answer: A
In Alice
functions and methods both may have return statements
functions must have return statements but they are optional in methods
functions always have return statements and methods never do
functions and methods always have return statements
Answer: C
Bouncing ball demo main points
Scenario: ball is thrown up (and a bit to the right), comes down under
the force of gravity, and bounces up again when it reaches the ground
A Y velocity variable is modified each time around the loop and when the
ball bounces
rather than modifying a time variable
The program loops indefinitely
A conditional test (if statement) determines when the ball hits the ground
A conditional statement nested in the then-part of another conditional
statement detects if the ball is near the (center of) the circle, in
which case it becomes invisible
The loop continues indefinitely, even though there is no visible change
after the ball falls into the hole
The program is made to stop when the ball falls into the hole by
replacing the infinite loop with a while loop with a test that calls the
ball's isShowing function
the loop body is unchanged
While statement flow of control
In words:
While condition is true
execute statement(s)
If the condition is never true, the statements are never executed
If the condition is always true, the loop is infinite (it never ends)
Avoiding infinite loops
Though sometimes infinite loops are desired, they frequently are the
result of programming errors
A while loop will be infinite unless the statements in its body change
something so that makes the condition false
assuming the condition is true to begin with
and assuming no events or other external control events do not make a
change the condition depends on
Loop how many times?
Infinite loop: infinitely many times
in Alice, use Loopinfinitytimes
another way: Whiletrue
Definite loop: a fixed number of times
known when the program is written, when the loop begins
in Alice, use a Loop statement
Indefinite loop: an unknown number of times
determined by some condition that occurs while the loop is running