CompSci 18S Recursion Dec 4, 2006 Prof. Susan Rodger.

Slides:



Advertisements
Similar presentations
Recursion Alice.
Advertisements

AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
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.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
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.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
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.
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.
Fall 2009ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
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.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
CompSci 4 Chap 7 Sec 2 Apr 7, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
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.
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
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.
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.
Mathematical Expressions, Conditional Statements, Control Structures
CompSci 4 Chap 5 Sec 2 Oct 18, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
CompSci 4 Chap 4 Sec 1 Sept 15, 2005 Prof. Susan Rodger.
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.
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.
CS320n –Visual Programming Definite / Counted Loops (Slides 7-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CompSci 4 Chap 5 Sec 2 Oct 9, 2008 Prof. Susan Rodger.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
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.
February 25,  The BDE(Begin-During-End) event.  Worksheet – Exercise # 9 Instructions  2nd Period Test.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
CompSci 4 Chap 10 November 4, 2010 Prof. Susan Rodger.
CompSci 4 Chap 10 Nov 22, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
How Tall Are You? Introducing Functions for Alice 3 By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
Programming: Simple Control Structures
Chapter 3: Decisions and Loops
Programming: Simple Control Structures
Recursion Alice.
While: Indefinite Loops
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Ch 7: JavaScript Control Statements I.
Let's Race! Typing on the Home Row
Recursion Alice.
Programming: Simple Control Structures
Programming: Simple Control Structures
Programming: Simple Control Structures
Programming: Simple Control Structures
Computer Science Core Concepts
Programming: Simple Control Structures
The structure of programming
Thinking procedurally
Programming: Simple Control Structures
How to allow the program to know when to stop a loop.
Presentation transcript:

CompSci 18S Recursion Dec 4, 2006 Prof. Susan Rodger

Repetition Sometimes don’t know exactly how many times a block of instructions should be repeated –Repeat until some condition is true –Repetition gets closer to condition being true Example – Chess, don’t know when in advance how many moves til game ends

Indefinite Repetition When number of repetitions is indefinite –While statement – previously –Recursion - today

Recursion Many times a structure is identified by a special word –Do in order –Do Together –If/Else –Loop Recursion –Is NOT a program statement with a special word –Recursion means a method (or function) calls a clone of itself

Example – horse race Horse race One horse randomly selected to move forward, repeatedly First horse to finish line is winner

Storyboard “do everything again” means the entire method should be repeated – this is recursion race if one of the horses has won winner says “I won” else randomly choose horse and move do everything again

“Do everything again” - Call race method Recursion means that a method calls a “clone of itself” race if one of the horses has won winner says “I won” else randomly choose horse and move call the race method

Stepwise Refinement race if one of the horses has won winner says “I won” else randomly choose horse and move call the race method isGameOver? whichHorseWon? moveRandomHorseForward

isGameOver and WhichHorseWon isGameOver –Is the finish line < 0.5 meters in front of any horse? If so, game is over –Returns true if game is over WhichHorseWon –Which horse is within 0.5 meters of finish line? – Returns the horse that won

moveRandomHorseForward To choose horse to move forward, use built-in random selection function

race method Uses recursion Where is the “way out”?

Classwork today Written: raceHorse move recursive statement Write recursive method BunnysMeet More info on Recursion: See Chap. 8, Sec 1