Lesson 8: Recursion vs Iteration
45 minutes
Overview
What is the difference between recursion and iteration?
Students explore how iterative solutions can be implemented using recursion and identify the similarities and differences in both algorithms. Students practice converting iterative algorithms to recursion then continue development on their Creative Coding with The Theater Project.
Standards
CON-2 - Programmers incorporate iteration and selection into code as a way of providing instructions for the computer to process each of the many possible input values
CON-2.O - Determine the result of executing recursive methods.
- CON-2.O.5 - Any recursive solution can be replicated through the use of an iterative approach.
Agenda
Objectives
Students will be able to:
- Convert between iterative and recursive algorithms
- Identify the base case and the recursive case in an algorithm
Preparation
- Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the students
- U7L8 Extra Practice - Handout
Teaching Guide
Warm Up (5 minutes)
Recursive Navigation
Remarks
In the previous lesson, you learned that recursive methods are methods that call themselves and that they consist of a base case and a recursive case. Recursive methods can solve the same problems as iterative solutions. Let's look at how we might use recursion to create drawings.
Group: Place students in pairs.
Do This: Have students identify the base and recursive case for the problem. Have students share with another pair, then have students share their solutions with the class.
Do This: Click through the animated slide to identify the base and recursive cases.
Activity (35 minutes)
Recursion vs Iteration (20 minutes)
Remarks
Recursive and iterative algorithms can be used interchangeably. Any iterative solution can be implemented using recursion, and any recursive solution can be implemented using iteration. Let's explore why we would want to use a recursive approach over an iterative approach and how we could translate between algorithms using recursion and algorithms using iteration.
Do This: Review the lesson objectives.
Group: Place students in pairs.
Do This: Direct students to Level 1 on Code Studio to investigate the program with a partner. Students make the changes to the program as prompted.
Discuss: Click through the animated slide to display the prompts.Use the Retrieve-Pair-Share strategy to discuss the prompts.
- What is the base case for reversing an array?
- Which parts of the iterative solution represent this base case?
- What is the repetitive process in the iterative solution?
- How can we make this process occur recursively?
Discussion Goal: Students identify the base case as when the start is greater than or equal to the end. Students notice that the condition in the loop represents the base case. Students identify the repetitive process in the iterative solution is that the index increases by one until the end of the array is reached. Students suggest that the recursive solution would call itself with the parameter increased by one.
Do This: Click through the animated slide to demonstrate translating the base case from the iterative version of the algorithm to the recursive version.
Do This: Click through the animated slide to demonstrate translating the recursive case from the iterative version of the algorithm to the recursive version.
Do This: Direct students to Level 2 on Code Studio to complete a choice level to translate an iterative algorithm to a recursive algorithm using the console, The Neighborhood, or The Theater.
Project Development (15 minutes)
Remarks
Using the items on your Project Planning Board, continue to develop your Creative Coding Project. Each time you complete a goal, commit your code and move the tasks to the next column. There should only be one item in the “In Progress” column at a time, meaning that each item should be entirely completed before moving on to the next item.
Do This: Direct students to Level 3 on Code Studio to work on their Creative Coding with The Theater Project.
Remind students to refer to their Project Planning Board to decide what tasks to work on during this time. You can also encourage students to request a code review from their peers as they work.
Do This: Have students update their Project Planning Board and Project Backlog with any tasks they completed, changed, or added.
Wrap Up (5 minutes)
Revisiting the Need to Knows
Remarks
We just learned a lot of new information today, which may have even answered some of the Need to Know questions you wrote down about the Creative Coding Project. As we progress through the unit, it is helpful to stop and note what we have learned that is related to or useful for the project.
Do This: Have students review the questions they wrote in the "Need to Know" column on page two of their Creative Coding with The Theater Project Planning Guide. Students add new questions to this column, check off any answered questions, and write answers to any questions in the "Learned" column.
Do This: Have students share what they added to their chart with a partner.
If time permits, you can also have students share as a class.
Assessment: Check for Understanding
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
AP Classroom Topic Questions
To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.