16
Programming: Simple Control Structures Alice

Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Embed Size (px)

Citation preview

Page 1: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Programming: Simple Control Structures

Alice

Page 2: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Control Statements

We 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

repetition

Page 3: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Conditional Execution

Conditional execution is where some condition is checked and a decision is made about whether a block of the program will be executed.

Conditional execution is extremely useful in games

simulations

real-time controls, e.g. robot systems

Page 4: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Example

As a simple example of conditional execution, let's revisit the FirstEncounter world.

As the robot moves closer, the alien hides behind the rocks.

We want to check a condition (is robot to short to over the rock?) and then perform an action based on the whether the condition is true.

Page 5: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

If/Else

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

Page 6: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Storyboard

A storyboard design for this conditional statement is:

The condition in an If statement is a Boolean function that yields a true or false value.

If spiderRobot is shorter than rock Do in order

spiderRobot neck move upspiderRobot neck move down

Else Do nothing

Page 7: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Demo

Ch03Lec2FirstEncounterIfElse

Concepts illustrated in this example program:

The condition of an If/Else statement is constructed by using a function that returns a Boolean value (true or false).

Do nothing in the Else part of the statement means that if the condition is false, Alice will skip this part and go to the next instruction.

Page 8: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

A different scenario

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

For example, the built-in function is shorter than compares the heights of two objects.

Suppose, however, that we wanted to compare the robot's height to 2 meters.

How can we compare the height of the robot to a specific measurement (2 meters)?

Page 9: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

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

Page 10: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Demo

Ch03Lec2FirstEncounterRelationalOps

Concepts illustrated in this example program: Relational operations are defined using the world'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 by either a function or a number.

Page 11: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Need for RepetitionThe walking action is not realistic: the robot moves forward to the rock ( a distance of several meters) but the legs are simulating a walking action only once.

Page 12: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

One meter, one step

A more realistic action is to move the robot forward one 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 and again to animate the robot moving close to the rock.

Page 13: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Loop

The Loop statement is a simple control structure that provides for repeating an instruction (or block of instructions) a counted number of times.

Page 14: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Demo

Ch03Lec2FirstEncounterLoop

Page 15: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Assignment

Read Chapter 3 Section 2 Simple Control Structures

Read Tips & Techniques 3 Engineering Look & Feel

(Texture Maps, Fog)

Page 16: Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed

Lab

Chapter 3 Lab Lec2