On this mastery I’ll show you when to use what type of repetition in a program.
For the purpose of this mastery, we’ll focus on while and for loops because they are the easiest and most used ones.
On mastery 19 we learned how to use a while loop. The while loop consists of something that will happen WHILE a condition is given. For example: WHILE x is greater or equal than 0, something will happen, and this process will continue to happen WHILE this condition is true.
On mastery 20 we learned how to use a for loop.
A FOR loop is a type of loop which allows you to give specific conditions to it, and then repeat when this conditions are true.
Now, while and for may be seem like they are the same, but they certainly ain’t. The main difference between them is that FOR loops will continue happening for a given number of iterations.
As you may recall, functions often use a for loop because of the iteration part. In someway, FOR loops are “better” when dealing with functions, while WHILE loops are great to work with inside main().
Picture and more information from this webpage.
Matery 22 by Omar Peza is licensed under a Creative Commons Attribution 4.0 International License.