In this lesson we’re going to talk about while loops. While loops are a great way to make a robot or computer do things over and over again for a number of times before moving on to other code.
Here’s what we’ll be covering in this lesson:
Once the statement has run, the condition will be checked again. If the condition is true, then the while loop will run the statement again. The robot or computer will do this again and again until the condition is false.
Image that we a stereo in our condition for the while statement. If the stereo is on (it is true) and there is music playing a robot will show up and dance.
As long as the music is playing the robot will stay in the same place and dance- it can’t go do anything else because it likes dancing to the music so much. The second the stereo is turned off (meaning it is false) the robot will wander off to do all the other tasks that are in its code.
You have to be careful with while loops because if the answer to the condition is never false the robot will never stop dancing or doing whatever code is inside the while statement. That means that the robot can get stuck and never do any code outside of the while loop!
- While Loops
- A While Loop with Sparki
- Getting Stuck in a While Loop
- Getting Stuck in a While Loop with Hardware
While Loops
While loops are similar to if statements. The difference is that instead of running the statement code once like in if statements, they will continue to run the statement code forever until the condition isn’t true anymore. This means that any code inside of the curly brackets will run over and over and over until whatever question (or questions, because you can ask more than one question) is inside of the parentheses is equal to false. Here’s what a while loop looks like in pseudocode:
1 2 3 4 |
while (condition) { statement } |
1 2 3 4 |
while (stereo plays music) { robot dances } |
Try it with Sparki
This Sparki code is an example of a while loop. As long as something is directly in front of Sparki’s distance sensor (less than or equal to 10 centimeters), Sparki will move forward a little bit. It’s almost like it’s chasing your hand. Try it out!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <Sparki.h> // include the sparki library void setup() { } void loop() { while( sparki.ping() < 10) { sparki.moveForward(1); } } |
Gettting Stuck in a While Loop
This Sparki code is an example of a while loop that will cause Sparki to get stuck in a while loop. Sparki will move forward for a while until the variable loopCount equals ten, but after that Sparki will never exit the while loop again and will continue to spin in circles until its battery runs out or you help the poor robot by turning it off!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <Sparki.h> // include the sparki library int loopCount = 0; // variable to count loops void setup() { } void loop() { sparki.moveForward(10); while( loopCount == 10) { sparki.moveRight(); } loopCount = loopCount + 1; // count the number of loops } |