(The credit of the background images goes to https://flic.kr/p/dAus13 and https://flic.kr/p/hiFuew )
So this are Masteries #19 & #20 and I’m going to tell you about the loops while and for.
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:
On the other hand, you use the loop is used it when you want to write a lopp that needs to execute a specific number of times.
The syntax you need to follow is the next one:
for ( variable; condition; increment) {
statements
}
For the next video, I used the same example of above, but with the changes of the syntax of for:
And again, you can observe that if I hand’t broke the loop, it would’ve been an infinite loop!
MASTERIES #19 & #20: Use of loops with “while” and “for” by Ana Gloria Angulo is licensed under a Creative Commons Attribution 4.0 International License.