Upload
simon-ellis
View
217
Download
0
Embed Size (px)
Citation preview
Programming: Simple Control Structures
Part 1 – Conditional ExecutionAlice
Control Statements
You 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
whether or not an instruction(s) is executed
repetition execution of the same instruction(s) more than once
This session will focus on conditional execution.
Conditional Execution A conditional execution is where some condition is checked and a decision is made about whether a section (block) of the program will be executed.Conditional execution is an extremely useful program component in
games simulations real-time controls, for example robot systems
Example
As a simple example of conditional execution, let's revisit the Egyptian scene in the Hollywood movie set.
The camera angle can mislead our perception of the scene
Which is taller – the mummy or the pharoah?
If/Else
We need to check a condition (is the mummy taller than the pharoah?) and then perform an action based on the whether the condition is true.
In Alice, an If/Else control statement is used to check a condition and make a decision.
Storyboard
In our example, we can demonstrate which person is the tallest by having that person turn around. A design for this action is:
The condition in an If statement is a question that yields a Boolean (true or false) value.In this example, is taller than is a built-in question.
If mummy is taller than pharoah mummy turns 1 revolutionElse pharoah turns 1 revolution
Demo
Develop program in classroom demonstration.
Caution!
Checking a condition can be tricky because it is easy to forget about some possibility.In our example, we tested for whether the mummy is taller than the pharoah. If not, we assumed that the pharoah was taller than the mummy.What if the pharoah and the mummy are of equal heights?
Nested Ifs
To resolve this issue, we can use two If statements, one inside the other (we call this nested Ifs).
A storyboard:If mummy is taller than pharoah mummy turn 1 revolutionElse If pharoah is taller than mummy pharoah turn 1 revolution Else Do together mummy turn 1 revolution pharoah turn 1 revolution
Demo
Modify program in classroom demonstration
A different scenario
In some cases, the built-in questions are not sufficient for a condition that we want to check.
For example, the built-in question is taller than compares the heights of two objects. We used this question to compare the heights of the mummy and the pharoah.
Suppose, however, that the casting call for the mummy requires that the actor is than 2 meters tall.
How can we compare the height of the mummy to a specific measurement (2 meters)?
Relational OperatorsIn 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 questions.
Using a relational operator
To demonstrate whether the mummy's height is more than 2 meters, we can use the > operator:
Assignment
Read Chapter 3-2 (through p. 66), Simple Control Structures – Conditional Execution
Lab 3-2 (part 1)