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

Slides:



Advertisements
Similar presentations
AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
Advertisements

Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
While: Indefinite Loops Sec 8-14 Web Design. Objectives The student will: Understand what an Indefinite Loop is Understand how create an indefinite loop.
Programming: Simple Control Structures Part 1 – Conditional Execution Alice.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Introducing While loops (and random numbers too) Alice.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 3 Programming - Putting Together the Pieces.
Execution Control with If/Else and Boolean Functions Example: Single Condition Alice.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Execution Control with If/Else and Boolean Functions
Execution Control with If/Else and Boolean Questions Part 1 Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Functions Alice.
Programming: Simple Control Structures Part 2 – Repetition Alice.
Mathematical Expressions, Conditional Statements, Control Structures
Programming: Putting Together the Pieces Built-in Questions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Today’s Agenda 1.Collect Pre-Lab 3 2.Alice Programming Assignment 3.Built-in Functions 4.Expressions 5.Control Structure 6.Assign pair programming teams.
Programming: Simple Control Structures
Programming: Simple Control Structures MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Programming: Simple Control Structures Sec 46 Web Design.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Obj: to recognize built-in functions and expressions HW: Read Section 3.1 and do HW4 Edmodo article due Sunday at 8pm Edmodo feedback due 8pm 10/17 Do.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Functions Sec 51 Web Design.
Programming: Simple Control Structures
Programming: Simple Control Structures
While: Indefinite Loops
Simple Control Structures
Functions Sec 8-11 Web Design.
Repetition Control Structures
Programming: Simple Control Structures
IF if (condition) { Process… }
Repetition: Definite Loops
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Design and Implementation
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Repetition: Definite Loops
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Functions Alice.
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Presentation transcript:

Programming: Simple Control Structures Alice

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

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

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 We want to check a condition (is the mummy taller than the pharaoh?) and then perform an action based on the whether the condition is true.

If/Else 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 object is the tallest by having that object turn around. A storyboard design for this action is: The condition in an If statement is a Boolean function returning a Boolean (true or false) value. If mummy is taller than pharaoh mummy turns 1 revolution Else pharaoh turns 1 revolution

Demo MummyIfElse 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.

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 taller than compares the heights of two objects. We used this function to compare the heights of the mummy and the pharoah. Suppose, however, that the casting call for the mummy requires that the actor is more than 2 meters tall. How can we compare the height of the mummy to a specific measurement (2 meters)?

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.

Demo Ch03Lec2mummyRelationalOps 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.

Example Let's write code to make the mummy "walk" – a somewhat stilted motion like you would see in an old Hollywood horror film. The code will be more complex because we need 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

Need for Repetition In this example the mummy takes 1 step forward on each leg. Suppose we want the mummy to take 20 steps forward?

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

Assignments 1. Snowman to stool 1. Place a snowman/woman and a stool in a grass world. Make sure there is distance between the snowman and stool. 2. Make the snowman/woman move toward the stool one meter at a time (you MUST use a loop—use the distance function to determine the number of times the loop repeats). 3. Make sure you test your solution by placing the starting point of the snowman/woman in various places in your world.