Do you know what is a loop? How might it be used in programming?
There are several types of loops that you can create with Snap!
1. Forever loop
Just like their name says, forever loops repeat the code within them forever. If you're suspicious about this, look at the bottom of the forever loop block. It's missing the connector at the bottom, so the code will never exit out of the loop and move on.2. For loop
For loops repeat the code within them for an amount of time that you set. Once the code has been repeated for that amount of time, your program will exit out of the loop and move on to the rest of your code. The block in the program is called "Repeat".
3. Until loop
Until loops repeat until the condition you set in the diamond/hexagon-shaped space is true. Each time the program runs through the repeat until loop, it checks to see if the condition is true. If the condition is true, that doesn't mean the code automatically stops! The code stops when the condition is true AND when the code in the loop is finished. The bloc is called " Repeat Until".
You have three programs that you need loops for. Which loop would you use for each program? Each type of loop is used only once. Discuss with your classmates:
- For playing your favorite song three times
- For flying your CoDrone until the space key is hit
- For keeping a digital clock running
Activity: Stair challenge
Make your CoDrone fly up a set of stairs (fly up, forward, up, forward, etc) and then fly back down!
- Use variables for delays.
- Use loops for any code that will be repeated more than once. Hint: you should have two loops in your program!
- Your CoDrone has to fly up at least three stairs.
- Your CoDrone needs to land!