While Statements

Keeping the Robot Busy

SparkiDuino
Easy

Lessons You Should Know

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:

 

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: 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. robotGetDownYou 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!  

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!

 

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!

  You might expect Sparki to snap out of the while loop after loopCount gets another one added to it, but Sparki is stuck inside the while loop! loopCount will never get one added to it ever again after it equals ten because that code is outside of the while loop. Sparki will never reach that code! Be very careful when you are writing while loops. Make sure that the condition you are checking can be changed either by a sensor or by code that is inside of the while loop statement code. Otherwise Sparki will get stuck!  

Gettting Stuck in a While Loop with Hardware

There’s another way to get stuck in a while loop! You may have written the while loop code perfectly, but for some reason your robot still gets hung up on a while loop. You’ll scratch your head trying to figure out what’s going on but SparkiDuino won’t offer you an error messages as clues. Look at your code. Do you have a sensor value in your condition? Often people forget to recheck their sensor values from inside the while loop. We kind of covered that above, but what about forgetting to turn the wheels? People also forget to add the code that will get their robot to a place where the sensor values will change. If Sparki needs to move in order to get a different sensor reading, if you don’t put that code inside your while loop Sparki will stay stuck wherever it first entered the while loop! Getting stuck inside loops is a very common error. Be careful whenever you are using code that creates loops to make sure it doesn’t happen to you! (It will eventually, but hopefully you’ll remember your time with Sparki and fix it quickly!) Time to move on! Next up we’ll learn how to make a choice depending on the value of an integer variable.  

Recap of What We’ve Covered So Far

or

Next Step:

Now that you know how to make a while loop, we will see how to use a switch statement.

Next Lesson – Switch Statements