Making decisions - guyjbrown/bleepmanual GitHub Wiki

We can make decisions in Bleep using the if statement. Try this:

value = 2
if value<5 then
   sample("drum_heavy_kick")
end

When you copy this into Bleep and run it, you'll hear the kick drum play. Why? If the condition after if evaluates to true, the statements inside the if block (between then and end) are executed. Here, the condition is value<5. This evaluates to true, since value contains 2 and 2<5. So the kick drum sample is played.

We can make comparisons using several different operators such as < (less than), > (greater than) and == (equals). Here are some more examples:

value = 4
if value>10 then
   sample("bass_drop_c")
end

Did you hear anything? No, because the condition evaluates to false in this case.

We don't have to make decisions base on numbers, we can test the value of strings too:

status="go"
if status=="go" then
   sample("elec_ping")
end

🚀 Now experiment! Try writing if statements with different conditions and see what happens.

Finally, we can use if-else to decide between two options. Let's modify the previous example so that we play one sound or another, depending on the value of the variable status:

status="stop"
if status=="go" then
    sample("elec_ping")
else
    sample("elec_flip")
end

Now the statement after the word else are executed if the test evaluates to false. So if status is "go" we make a "ping" sound, otherwise we make a "flip" sound.

🚀 Try changing status to different values and see what happens.

Finally, in each part of the if statement we can actually have a sequence of statements. So we could decide between playing two sequences of sounds, not just single sounds.

use_bpm(100)
status="go"
if status=="go" then
    sample("elec_ping")
    sleep(0.25)
    sample("bd_tek")
else
    sample("elec_flip")
    sleep(0.25)
    sample("bd_sone")
end

Next: Random numbers