Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Recursion Alice

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

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

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

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

6 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

7 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

8 Assignment Read Chapter 8 Vocabulary Words HorseRace Example Towers Example Page 253, #2 Reversal Exercise (worksheet)


Download ppt "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."

Similar presentations


Ads by Google