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.

Slides:



Advertisements
Similar presentations
More About Recursion Chapter 8 Section 2. Recall Recursion is a programming technique where a method calls itself. Recursion uses the IF/ELSE control.
Advertisements

Recursion Alice.
AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
Class-level Methods and Inheritance Part 1 Alice.
Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
Repetition Structures
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Fall 2009ACS-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.
CompSci 18S Recursion Dec 4, 2006 Prof. Susan Rodger.
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.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
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.
CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 7 Repetition.
Tips & Techniques 6 Random Numbers and Random Motion Alice.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Recursion Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition.
More About Recursion Alice. A second form of recursion A second form of recursion is used when the solution to a problem depends on the ability to break.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
CompSci 4 Chap 7 Sec 2 Apr 7, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Recursion Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Programming: Simple Control Structures Part 2 – Repetition Alice.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
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.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Lists Alice. Collections In some animations, several objects must perform the same actions Example: A marching band, where the band members are performing.
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.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
Programming: Simple Control Structures Sec 46 Web Design.
List Search Alice. Common Uses of Lists Iterating through a list of several like items to accomplish the same task with each item. As in the previous.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Tips & Techniques 6 Random Numbers and Random Motion Alice.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Programming: Simple Control Structures
Programming: Simple Control Structures
Recursion Alice.
While: Indefinite Loops
Repetition Control Structures
Recursion Alice.
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Repetition: Definite Loops
Functions Alice.
Programming: Simple Control Structures
More About Recursion Alice.
Functions Alice.
Programming: Simple Control Structures
Class-level Methods and Inheritance
Functions Alice.
Functions Alice.
Presentation transcript:

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 know is that repetition is needed For example, in a board game like chess or checkers, we don’t know exactly how many moves it will take for a player to win or lose the game – all we know is that several moves will be needed.

Indefinite Repetition In programs where a count of repetitions is not known (indefinite), we can use one of two repetition control mechanisms: While statement Recursion This session focuses on the While statement.

How the While statement works The general idea is: While some condition is true execute instruction(s) To write a While statement, we need to know the condition that determines whether the loop will be repeated.

Example A common feature in popular "action films" is an exciting chase scene. As an illustration of an animated chase scene, consider the hungry shark in this world. The shark is going to chase after and catch a fleeing fish.

Problem The problem is how do we get the shark to chase the goldfish in a chase-like action? The shark should not immediately catch the goldfish (otherwise, there would be no chase). The goldfish (assuming self-preservation instincts) should appear to be fleeing.

Solution To create a chase scene, At the same time, the shark will swim a short distance toward the fish and the fish will swim a short distance away from the shark. The fish will flee to a random (but nearby) location. As long as the goldfish is still 0.5 meters away from the shark, repeat the actions.

Storyboard chase While the goldfish is more than 0.5 meters away from the shark Do in order shark point at the goldfish Do together shark swim (toward the goldfish) goldfish flee (away from the shark) shark eat (the goldfish) The shark swim, goldfish flee, and shark eat actions are complex. Use stepwise refinement to break them down into simple steps.

chase While the goldfish is more than 0.5 meters from the shark Do in order Point the shark at the goldfish Do together shark swim goldfish flee shark eat (goldfish) swim tDo in order urn torso left and move forward turn torso right and move forward turn torso left and move forward flee Do together wiggle tail move to random location Eat Parameter: what Do in order shark points at what shark opens jaw and what disappears shark closes jaw

Demo Ch07Lec2Chase Concepts illustrated in this example A While statement uses a Boolean condition to determine when the repetition ends. Code written in a previous program can be reused in a new program. In this example, the flee method calls the previously written randomMotion method.

Shark will catch goldfish How do we know the shark will eventually catch the goldfish? The shark always moves 0.4 meters toward the goldfish The goldfish's random motion is restricted by the min and max values used in the random number function.

The loop will end Geometrically, the fish can never move more than 0.35 meters away The shark has a distance advantage and will eventually catch up. The loop will end

General “Rule of Thumb” As a general rule, a While loop should be written so the loop will eventually end. Requires that statements within the loop change the conditions of the world such that the condition for the While statement will eventually become false. If the While loop never ends, it is an infinite while loop.

Assignment Read chapter 7-2, Indefinite While loops Read Tips & Techniques 7, The Begin During End (BDE) Event

Lab Chapter 7 Lecture 2 Lab