AAP-2.J: Students will be able to express an algorithm that uses iteration without using a programming language.
AAP-2.J.1: Iteration is a repeating portion of an algorithm. Iteration repeats a specified number of times or until a given condition is met.
AAP-2.K: Students will write and determine the result of iteration statements.
From the APCSP course description: The way statements are sequenced and combined in a program determines the computed result. Programs incorporate iteration and selection constructs to represent repetition and make decisions to handle varied input values.
The objective of this lesson is to introduce students to iteration in programming. Programmers can more efficiently implement repeating lines of code by condensing them into iteration statements, such as by repeating statements a certain amount of times or until a certain condition is met. Students will use iteration to loop the drum beats that they have created in previous lessons in order to play them over more measures and implement them into more structured songs.
Activity 5.4.1 (Budget 30 minutes)
Introduce the term "Iteration" to the students. Iteration is a process of repeating a set of instructions until a certain condition is met. A real-life example of iteration is when you are trying to find a specific book in a library. You would start by looking at the first shelf and checking if the book is there. If it is not, you would move on to the next shelf and repeat the process until you find the book or you have checked all the shelves.
Have students pair up and share an algorithm that uses iteration without using a programming language.
Navigate to the Iteration Slide Deck to introduce Iteration in EarSketch.
Breakdown of a for loop in Earsketch
Provide a premade Earsketch script to students that they are tasked with rewriting/restructuring using iteration.
After presenting the slide deck, open the provided template script and give students instruction
Script will initially consist of a long sequence of makeBeat() statements, and students must identify the two separate song sections to create two for loops
Note that the counter variable can be initialized to numbers other than 1 (see solution script)
Walkthrough of solution.
Section A will loop over measures 1-4 and section B will loop over measures 5-8
Activity 5.4.2 (Budget 30 minutes)
Students loop the drum beat from the script created in lesson 5.1
Nearly all the code can be kept the same, only the makeBeat() needs to be moved into a for loop
Ask students to loop the drum beat for 8 measures
Students add accompaniment to their drum beat
Using fitMedia(), students should add 2-3 instrumental tracks to their script
This exercise will be the foundation for the songs that they create for the Unit Challenge, as one of the specifications is that their music should include looped drum beats
Class discussion - if a program contains a loop that iterates over a list of numbers and calculates their sum, students should be able to determine what the final value of the sum will be after the loop has completed. In other words, they are identifying the side effect that occurs during the execution of the loop, such as modifying the values of variables outside the loop. Have students determine the result of their loop.