Captura de pantalla 2015-11-23 a las 21.20.51
By Thomas Harper

This is mastery 22. I’m really excited because, after this one, I have to do just one more mastery!

In this post, I’ll write about repetition types, which are used when you want the program to do the same thing, again and again, under a certain condition.

There main repetition types are:

For loop

They are compose by three parts:

  1. Initialization variable-  value in which the loop will start.
  2. Condition- when te loop will end.
  3. Action- usually increment or decrement a variable.

You should use a for loop when you want to give a certain value for the loop to start and a certain action.

Here’s an example of a code where I used a for loop:

https://github.com/mferflores/fibonacci/blob/master/function

While

You should use the while loop when you want the program to perform the loop only when te condition is true.

Here’s an example of a code where I used a while loop:

https://github.com/mferflores/Babylonian/blob/master/Square%20root

Do/while

You should use the do/while loop only when you want the loop to be performed at least once.

Here’s an example of a code where I used a do/while loop:

https://github.com/mferflores/WSQ14/blob/master/Estimating%20e

 

And here’s a video with a summary:

CC BY 4.0 When to use what type of repetition by Mferflores is licensed under a Creative Commons Attribution 4.0 International License.