110 likes | 139 Views
Learn how to identify code blocks, loop components, and demonstrate loop functionality by adding repeated sounds to a composition. Understand the importance of loops in creating efficient code.
E N D
CS111 Intro to Programming Day 11: Looping
Learning Objectives • Identify code blocks based upon their indentation. • Identify the components of a loop • Demonstrate loop functionality by adding repeating sound to a composition
Déjà vu - Didn’t I already do this? Computers executes commands in a particular order called the control flow. If we want to duplicate an action over and over again, we could copy the statements as many times as we need… OR…. we could use a loop! A loop instructs the computer to execute a section of code repeatedly, creating more efficient code.
Loop components Python provides several types of loops and in Chapter 12 we learn about the for-loop. For-loop Components: • Loop counter (variable) • Range ( values assigned to loop counter variable each time through the loop (iteration) ) • Loop body (code to execute repeatedly)
Loop components Loop Counter is just a variable that is set to a new value with each step (iteration) through the loop.
Loop components Range is function that returns a set of values over the specified range. The values range from the first value (inclusive) to the second value (exclusive).
Loop components Loop Body is the code statements to repeat with each iteration of the loop. Statements must be indented to be included in the loop body.
More Cowbell! The music business can be brutal… Sometimes you just need more cowbell! Saturday Night Live Skit