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.

Slides:



Advertisements
Similar presentations
Recursion Alice.
Advertisements

AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Repetition Structures
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.
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.
Arrays and Array Visualization
Fall 2007ACS-1805 Ron McFadyen1 Chapter 7 Repetition.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
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.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
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.
Variables and Inheritance A More Complex Example Alice.
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.
© Jalal Kawash Programming Peeking into Computer Science 1.
For loops in programming Assumes you have seen assignment statements and print statements.
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.
ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.
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.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
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.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
GRADE 9 Jeopardy (June) GeneralProgramming terms Alice programming ThinkingComputers in Society
Programming: Simple Control Structures
Arrays and Array Visualization
Programming: Simple Control Structures
Recursion Alice.
While: Indefinite Loops
Let's Race! Typing on the Home Row
Recursion Alice.
Programming: Simple Control Structures
Alice in Action with Java
Doing things more than once
Repetition: Definite Loops
Programming: Simple Control Structures
Design and Implementation
Programming: Simple Control Structures
Programming: Simple Control Structures
Repetition: Definite Loops
Functions Alice.
Programming: Simple Control Structures
More About Recursion Alice.
Functions Alice.
Programming: Simple Control Structures
Functions Alice.
Functions Alice.
Variables and Inheritance A More Complex Example
Presentation transcript:

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 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: Recursion While statement This session focuses on Recursion.

Recursion Many of the pieces we use to create a program are identified by using special words. For example, Do in order Do together If/Else Loop Recursion is not a program statement with a special word that identifies it as part of the programming language. Recursion means that a method (or a function) calls itself.

Example – horse race A carnival style horse race. In repeated moves, one horse is randomly selected to move forward. The selected horse “runs” straight ahead to the finish line. A horse is the winner if it gets to the finish line before any other horse. When one horse wins, the game ends.

Storyboard "do everything again" means that the entire method should be repeated. To do everything again, the method calls itself. race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount do everything again

Stepwise Refinement race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount call race method isGameOver? whichHorseWon?moveRandomHorseForward

Demo Ch08Lec1HorseRace-V1 Concepts illustrated in this example In games, a conditional expression is often used to determine when a game is over. In this example, the game is over when one of the horses gets within 0.5 meters of the finish line. The same condition is used to determine the winner. Random numbers is used to introduce an element of chance into a game program.

Testing Testing a program that uses random numbers requires extra caution. In this example, we ran the program 20 times and found that racehorse1 won 7 times racehorse2 won 3 times racehorse3 won 10 times Something is wrong! Each horse should win approximately 1/3 of the time.

Demo Ch07Lec1HorseRace-V2 The bug in the first version of the program is that it has nested If statements, and a 33% probability for each If statement What we didn't consider is that if racehorse1 was not selected, then we have a 50% probability of selecting either racehorse2 or racehorse3.

Assignment Read Chapter 8 Section 1, Recursion

Lab Chapter 8 Lecture 1 Lab