<h2>CODING MUSIC</h2>
<hr />
<p><br />
<br />
Keyboard Guide</p>
<h2>Iterations (Loops!)</h2>
<p> </p>
<div style="padding:62.5% 0 0 0;position:relative;"><iframe allow="autoplay; fullscreen; picture-in-picture" allowfullscreen="" frameborder="0" src="https://player.vimeo.com/video/486951234" style="position:absolute;top:0;left:0;width:100%;height:100%;"></iframe></div>
<p> </p>
<h2>The Code Explained</h2>
<hr />
<h3> </h3>
<h3>Iteration</h3>
<p> </p>
<p>Sometimes in Sonic Pi you may want to do something multiple times. You could just type out your code over and over but there is a much easier way to do this and it is by using loops! <strong>Loops</strong> will allow you to reuse code and run it multiple times or even allow you to play sections until you press stop. Take a look at the example code below and decide which you would rather type this...</p>
<p> </p>
<pre style="border-radius:10px; color: #000000; border:25px solid #e5f7ff; outline:5px solid #ff00ab; background: #e5f7ff">
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span></pre>
<p> </p>
<p>...or this?</p>
<pre style="border-radius:10px; color: #000000; border:25px solid #e5f7ff; outline:5px solid #ff00ab; background: #e5f7ff">
<span style="color: #008000;">10.</span><span style="color: #400000;">times</span><span style="color: #800000; font-weight: bold;"> do</span>
play <span style="color: #008c00;">65</span>
<span style="color: #400000;">sleep</span> <span style="color: #008c00;">1</span>
<span style="color: #800000; font-weight: bold;">end</span></pre>
<p> </p>
<p>The second one is obviously easier to type and you will get the exact same result.</p>
<p> </p>
<p>Let’s break down the parts of a loop now. First, you have the <strong>times</strong> component which will tell your loop how many times you want to run the code contained in the loop.</p>
<p>Next, you have the <strong>do</strong> keyword which simply tells the computer that this is where the loop begins. Now you can type whatever code you want inside the loop to be repeated.</p>
<p>It is important that you have the tabs or whitespace so that your code is readable. To push those lines over, use the <strong>tab key</strong> on your keyboard.</p>
<p>Finally, you must end the loop by using the <strong>end</strong> keyword. Try putting all this together and give it a shot!</p>
<hr />
<h3> </h3>
<h3>Nesting Iterations</h3>
<p> </p>
<p>It is also possible to put a loop inside of a loop. This process is known as <strong>nesting</strong> in the computer science world. Look below to see an example of nesting:</p>
<p> </p>
<pre style="border-radius:10px; color: #000000; border:25px solid #e5f7ff; outline:5px solid #ff00ab; background: #e5f7ff">
<span style="color: #008000;">4.</span><span style="color: #400000;">times</span><span style="color: #800000; font-weight: bold;"> do</span>
<span style="color: #008000;">2.</span><span style="color: #400000;">times</span><span style="color: #800000; font-weight: bold;"> do</span>
<span style="color: #400000;">sample</span> :<span style="color: #005fd2;">elec_blip2</span><span style="color: #808030;">,</span> <span style="color: #005fd2;">rate</span>: <span style="color: #008c00;">2</span>
<span style="color: #400000;">sleep</span> <span style="color: #008000;">0.25</span>
<span style="color: #800000; font-weight: bold;">end</span>
<span style="color: #008000;">4.</span><span style="color: #400000;">times</span><span style="color: #800000; font-weight: bold;"> do</span>
<span style="color: #400000;">sample</span> :<span style="color: #005fd2;">drum_tom_mid_soft</span>
<span style="color: #400000;">sleep</span> <span style="color: #008000;">0.125</span>
<span style="color: #800000; font-weight: bold;">end</span>
<span style="color: #800000; font-weight: bold;">end</span></pre>
<p> </p>
<p>As you can see, we have put two loops inside of our main <strong>4.times</strong> <strong>loop</strong>. This means that we will hear the pattern inside the main loop play 4 times and each of those loops will play the respective amounts. I know this can be confusing at first but it really helps to unleash the power of code. An easy way to find out how many times a part of the loop will play is to multiple both loop numbers together.</p>
<p> </p>
<div style="background:#eeeeee;border:1px solid #ff00ab;padding:5px 10px;">🌟 <em>Remember that for every <strong>do</strong> keyword you must have an <strong>end</strong> keyword.</em></div>
<p> </p>
<hr />
<h3> </h3>
<h3>Looping</h3>
<p> </p>
<p>Iterations are great if you want to play something a set amount of times but what if you want to <strong>play something forever</strong>? This is where infinite loops come into play. The only way to stop these loops is to press the stop button. Take a look at the example below:</p>
<p> </p>
<pre style="border-radius:10px; color: #000000; border:25px solid #e5f7ff; outline:5px solid #ff00ab; background: #e5f7ff">
loop<span style="color: #800000; font-weight: bold;"> do</span>
<span style="color: #400000;">sample</span> :<span style="color: #005fd2;">loop_amen</span>
<span style="color: #400000;">sleep</span> sample_duration :<span style="color: #005fd2;">loop_amen</span>
<span style="color: #800000; font-weight: bold;">end</span></pre>
<p> </p>
<p>This loop will play the amen loop repeatedly forever until you stop it! What do you think will happen if you put some code after the loop? Let’s give it a shot with the following code:</p>
<p> </p>
<pre style="border-radius:10px; color: #000000; border:25px solid #e5f7ff; outline:5px solid #ff00ab; background: #e5f7ff">
loop<span style="color: #800000; font-weight: bold;"> do</span>
<span style="color: #400000;">sample</span> :<span style="color: #005fd2;">loop_amen</span>
<span style="color: #400000;">sleep</span> sample_duration :<span style="color: #005fd2;">loop_amen</span>
<span style="color: #800000; font-weight: bold;">end</span>
<span style="color: #400000;">sample</span> :<span style="color: #005fd2;">drum_cymbal_open</span></pre>
<p> </p>
<p>You should notice that the cymbal sample never plays. This is because the program never exits the loop and the sample is outside of the loop. This is an example of why you need to be careful when using infinite loops. Now you have the power of iteration and loops at your fingertips!</p>