Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming: Simple Control Structures Part 1 – Conditional Execution Alice.

Similar presentations


Presentation on theme: "Programming: Simple Control Structures Part 1 – Conditional Execution Alice."— Presentation transcript:

1 Programming: Simple Control Structures Part 1 – Conditional Execution Alice

2 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.

3 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

4 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?

5 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.

6 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 revolution Else pharoah turns 1 revolution

7 Demo Develop program in classroom demonstration.

8 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?

9 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 revolution Else If pharoah is taller than mummy pharoah turn 1 revolution Else Do together mummy turn 1 revolution pharoah turn 1 revolution

10 Demo Modify program in classroom demonstration

11 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)?

12 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 questions.

13 Using a relational operator To demonstrate whether the mummy's height is more than 2 meters, we can use the > operator:

14 Assignment Read Chapter 3-2 (through p. 66), Simple Control Structures – Conditional Execution Lab 3-2 (part 1)


Download ppt "Programming: Simple Control Structures Part 1 – Conditional Execution Alice."

Similar presentations


Ads by Google