CC licensed photo by Vox Efx on Flickr
CC licensed photo by Vox Efx on Flickr

Hello everybody, this post is to explain the basic use of while loops and for loops.

While is a loop that will be performed if the conditions inside its definition is true. Yes, you are right… again boolean value. If the condition inside the while returns the value true then the code under the while is performed.

For loop are more classy and I love them. Once you understand how it is used, a for loop is good for almos everything. For loops works with the next principle, you have a numer, you add orr substract from that number until a condition is met. For example:

for(i = 0; i<N; i++)

In this example first part befor the semicolon means the initial value of i is equal to cero, then in the second part before the second semicolon this means that the loop is going to be performed until i stops being less than N, the last part before the last brace means that i is going to increment by 1 each time the cycle is completed.

In the next videos this situation is better explained, the first video is mine and the second is someone’s else video I found on Youtube:

Maybe it is much better explained in this second video… just maybe

P.S.: Remember the words of the second’s guy video: a loop repeats a block of code, we are going in circles for a certain number of times.

CC BY 4.0 Mastery 19 & Mastery 20 by Octavio Rojas is licensed under a Creative Commons Attribution 4.0 International License.