# Lesson 4 # In this lesson, you will learn to use # some basic arithmetic operators, code # blocks, and iterators. # General Instructions: # # This lesson contains four examples and three # exercises (A, B, C). # # To listen to an example, change "comment" # to "uncomment" on the "comment do" line, # then click the "Run" menu command at the # top of the Sonic Pi editor window. # When you are done with the example, turn # it off by changing "uncomment" back to # "comment," and move on to the next section # of the lesson. # # Operators: +, -, *, / # Code Blocks: Do - End # Iterators: ___.times Do # Arithmetic Operators: In Lesson 3, we changed # rhythmic values using the arithmetic operators # * and / (multiply and divide). The operators # + and - (add and subtract) can also be useful. # Example 1: + (add), - (subtract) operators comment do play 60 + 2 # Play note 62 sleep 1 + 1 # Sleep for 2 beats play :C4 + 2 # Play note 62 sleep 1.0 - 0.5 # Sleep for half of a beat play :D4 - 2 # Play note :C4 (note 60) end # Exercise A: + and - operators. # Change the code in Example 1: # 1) First three lines: change each + to -, and # change the second term in each so that the # result is the same as: # # play 54 # sleep 0.5 # play :A3 # # 2) Last two lines: change each - to +, and # change the second term in each so that the # result is the same as: # # sleep 2 # play :F4 # Blocks. A block of code is simply a group of \ # lines of code framed by ____ do ...end commands. # Blocks are useful for organizing code into # efficient chunks that are easier to follow and # understand, and they often make it possible to # less code (more efficient) than code without # blocks. For example, using a "comment do" + # "end" or "uncomment do" + "end" block to prevent # or allow lines of code within the block to # execute is simpler and more efficient than # adding or removing "#" symbols for every # line of code. # Example 2: The comment block: comment do play :C sleep 1 play :D sleep 1 end # Exercise B: # 1) Add a # symbol to the beginning of the # "comment do" and the "end" lines in # Example 2, then Run. # 2) Add "#" symbols to the beginning of each # line of code within the comment do ... end # block of Example 2, then Run. # Notice how much more work this is than using # the comment / uncomment toggles for the block. # Iteration. An interesting and common way to use # blocks is to repeat lines of code a certain # number of times to achieve a result (iteration). # You can use a repeating block of code to create # a musical loop. In later lessons we will learn # how to create variations within a repeating # block. # Example 3 - Block Iteration (repeating code) comment do use_bpm 120 4.times do play :C sleep 1 play :C sleep 1 play :G sleep 1 play :G sleep 1 play :A sleep 1 play :A sleep 1 play :G sleep 2 end end # Exercise B - modify Example 3 so that the # block of code is repeated 3 times instead # of 4. For this exercise, copy and paste # or re-type the code below. Leave the # original Example 3 code unchanged so that # you can use it again in Exercise C. # Example 4 - Nested repeating code blocks. # You can repeat code blocks within other # code blocks: comment do 4.times do play :G sleep 1 2.times do play :C sleep 1 end end end # Exercise C - Example 3 contains repetitions # of lines of code: # play :C sleep 1 (twice) # play :G sleep 1 (twice) # play :A sleep 1 (twice) # # Using repeating blocks, rewrite these lines # so that they are repeated the correct number # of times within the larger block, but are # only written once in the code. Change the # outer block so that it repeats twice rather # than four times.