Video tutorial: [coming soon]

Blocks required:

Loop overview
Loops are used to go through one particular section of blocks several times. These can be useful when the user needs to go through a particular section multiple times, but the creator doesn't want to create the sections multiple times.

The example
In this example we are going to be using variables to build a basic loop system which has Alexa say the current loop number we're on.

This is what we'll be building:

Step one: stating the rules
We're going to use the 'Brian' Alexa voice for this skill. Let's connect the start block to the Speak block and rename it 'Rules'.

Step two: create the loop counting variable
To actually count how many cycles the user has gone through your loop, you need to create a variable for this. I created a variable called {loop_count}. You can do this by accessing your variables tab.


Step three: setting the IF block to count
For a loop to work, you need to have an IF block at the beginning, or end of your loop to count whether the number of loops you want to do has been hit. I typically put my IF blocks at the beginning of my loop. 


Step four: increase the {loop_count} variable
Next, we want to connect our IF block that starts the loop to a SET block which will increase the loop count variable once the user enters the loop. We want to connect the ELSE condition in the IF block to this SET block. We are saying "IF the user's {loop_count} variable is NOT 10, then activate the 'else' path". When the user's {loop_count} variable hits 10, then the #1 condition statement inside the IF block that says "IF {loop_count} = 10" will equate to true.

We can increase the {loop_count} variable by using the logic button in the SET block.

Step five: have Alexa speak the new loop count.
Using a SPEAK block with our {loop_count} variable, we want Alexa to speak out the current number of loops Alexa has performed. In our SPEAK block we can add our variable. 

At the end of this SPEAK block, we want to connect back to the IF block at the beginning of the loop. We do this to restart the loop.

Step six: end the loop
Now, when the user comes back to the beginning of the loop - the IF statement will check to see if the {loop_count} is equal to 10. If it isn't, then the loop will begin again and the {loop_count} variable will be increased by 1. 

After 10 loops {loop_count} will be equal to 10, and the IF statement will activate the #1 condition which asks "IF {loop_count} = 10". When that happens, the loop will end. 

Let's test it out in the Voiceflow tester! Below, we can see Alexa going through the loop 10 times - then hitting our final IF statement where it congratulates the user on making it through {loop_count} loops (10, in this case).

What's next for you and loops?
Hopefully this gave you a better sense of how you can add advanced loops into your Alexa skills.

Next, try asking users to give you a number and then perform that number of loops. You can do this by using a capture block to ask the user for a number, and setting your IF statement to the value of the number the user defined in the capture block.

Have fun looping!

Still have questions? Did we miss something? Have a suggestion? 

Did this answer your question?