Students identify repetitious code patterns.
Students learn to utilize a for loop to replace repetitious code to improve coding efficiency.
Students learn to use the EarSketch Code Indicator as a measure of improved coding efficiency.
The objectives for this section is to identify patterns of coding repetition, then replace the repetitious code with a for loop control structure to increase coding efficiency. For loops are used when the programmer knows how many times an action is to be repeated. EarSketch is designed so that coders will use a for loop to loop a makeBeat. It is also possible to use a for loop to loop musical sections of code if the sections are repeated in a clear pattern.
This is another demonstration of the applying the NGSS X-Cutting Concepts of Patterns, Structure, and Function. Previously, students examined patterns and structure/function concepts by comparing the components of a LEGO wind-up car to the components of a webpage resume and to musical structures. Put simply, programming code is a machine, just like a self-propelled LEGO car. The code, when assembled into specific structures, in a specific order, perform a desired function of efficiently engaging the listener. It is in this way, that code is like a machine.
In this section students will return to these X-Cuts to identify a pattern (repetition) in coding that affords the component assembled into the specific structure (for loop) to perform the desired function (efficiency). The table below maps this relationship.
Activity 3.15.1 (Budget 10 minutes)
Students identify repetitious code patterns.
1. Hand out the Repetitious Code worksheet and ask student pairs to identify the first set of repetitious code that they see on the page by putting a check mark in front of each line of the repetitious code. (They should put a check mark in front of the 4 makeBeats with beatString1).
2. Then ask them to circle what is different in the repetitious code. (They should circle the starting measures of the 4 makeBeats with beatString 1).
3. Then ask them to do the identify the next set of repetitious code with check marks in front of the lines of code and circle what is different. (They should put check marks in front of the 2 makeBeats with beatString2 and circle the starting measures).
Activity 3.15.2 (Budget 40 minutes)
Students learn to apply for loops to computationally loop a makeBeat.
1. Present the first example of the For Loop PowerPoint and facilitate a class response on the difference between the ending value of the repetitious code and the for loop.
2. Present the second example of the For Loop PowerPoint and facilitate a class response to confirm that students understand the difference between the ending value of the repetitious code and the for loop
3. Open the Repetitious Code Script and model in EarSketch how to loop the two examples of repetitious makeBeats using the Repetitious Code Script. When you are making the second loop, intentionally forget to change the starting measure to the loop counter variable in the makeBeat so that they can see that the code runs but it does not loop the beat. Then change the starting measure to the loop counter variable in the makeBeat so that it runs without a logic error. Show the DAW so that students can see the loop repetition visually. Save the script with another name so that you can show the value of the Code Indicator before and after replacing repetitious code in the next step.
4. Show the next to last two slides on how to use the EarSketch Code Indicator by right clicking the tab of a script and selecting Code Indicator. Model using the Code Indicator with the script you just saved in step 3 above (It should have a value of 20 because of the 2 loops). Open the original Repetitious Code script to show that the Code Indicator has a value of 0 when all of the code was repetitious.
5. Hand out the X-Cutting Concepts resource to students to show the relationship between the Lego Car, Musical Structure, and Coding Pattern, Structure, and Function.
6. Show the For Loop Mini Task on the last slide so that they each replace the rest of the repetitious makeBeats in the rest of the code.
3.15.1 Repetitious Code Worksheet (Student Worksheet)
3.15.2 For Loop PowerPoint (Teacher Resource)
3.15.2 X-Cutting Concepts Handout (Student Resource)
3.15.2 Repetitious Code Script (Teacher and Student Resource)