Iteration (iterate means to repeat) in programming is very important because many times we want to repeat something without actually repeat the instructions all the time.
Let's use the example of adding all numbers from 1 to 100 and output it. (Without doing directly using the math formula that you have seen in sequences)
We could write something like
(we go on until we arrive to…)
But, of course this is too slow and we don't want to write 102 lines of code.
So for making it better we have several ways to make this more palatable and easier to code.
We can use a loop while or a loop for
The loop while solution would be something like this
The loop for solution would be something like this:
As you can see we can reduce the amount of code from 102 lines to 7 or 5.
Now let's dive in a bit in the structure of both of them
The loop while has a condition that has to be met to enter the loop and after the code that is inside is executed is going to check if the condition is still met. In that case is going to execute the code inside the loop and then check the condition and so on.
Infinite loops
There is a posibility that we can enter in an infinite loop with this situation that it's never going to stop being executing. Sometimes this is what we want like in the "void loop()" of an arduino program. Also we can write "loop while true" to generat this type of infinite loop on demand.
//TO-DO