So this is Mastery #20 and I’m going to tell you about the loop while.

Let me tell you first that a loop is “is a way of repeating a statement a number of times until some way of ending the loop occurs”.  However, if you don’t tell your program when to stop, it will be a infinite loop. (http://cplus.about.com/od/glossar1/g/loopdefinition.htm)

You use the while loop when you want to repeat a statement as long as an expression is true.

This is the syntax you need to follow:

while (conditional) {

statement

}

Here’s a video of an example made by me:

As you can see I used break to stop the loop because if I hadn’t done that, it would’ve run forever as you can see in the next video:

CC BY 4.0 MASTERY #19: Use of loops with “while” by Ana Gloria Angulo is licensed under a Creative Commons Attribution 4.0 International License.