¿What happens when we are serving soda from a dispenser? Usually, we start our process by putting the glass below the dispenser and opening it in order to fill our cup, but it starts to fill out with foam, and we wait until it disappears and continues filling our cup if it is not yet at the desired level, and the process is repeated until we get the desired result.
The same way, when we cook a dish usually our recipe tells us to verify the state of our food, if it is not yet cooked we must wait sometime before to verify again, and repeat the process until we get what we need.
In a program’s execution it might be sometimes when we want to execute some set of instructions, one time after another while some conditions are met, these repeated executions are known as loop cycles or just loops, and they work very similar to conditionals.
There exist mainly 3 types of loops:
- For Loops
- While Loops
- Do – While Loops
All these share the same structure, which is composed of 2 parts:
- A condition o set of conditions
- A set of instructions to execute while the conditions are met
The execution of a loop statement usually starts with the validation of conditions, if these are met then the device executes the set of instructions within the loop, after that, it validates again the conditions and the process is repeated one time after other while the conditions return a true value, when they return false the execution of the loop is suspended.
Loops allow us to repeat the execution of a set of instructions, having a condition based on the values of a variable.
It means that for loops repeat their execution as far as a defined variable keeps between some predefined range of values.
For the while loops, we must define some conditions in the same way we do it with if blocks (conditionals) and the set of instructions will repeat its execution while the conditions remain true.
Do – while loops are very similar to while loops, the only difference relies on the fact that for these there will always be at least one execution of the set of instructions within the loop. That happens because the validation of conditions is done at the end of the first execution, in opposite to while loops where that validation is done before the first execution.
Going back to our example of crossing a street with a semaphore, if we write an algorithm with just an if -block, our program would verify the conditions just once, the device wouldn’t make the cross if conditions are not met and would remain stopped in front of the street. If we want it to verify again the conditions, we would need to execute the program again.
To solve it, in such a way that our program could verify one time and another until conditions are true to make the cross, we must rewrite our algorithm and use loops instead. For the example above our algorithm could be like the next image:
Break and Continue
The main mechanism we used to get out of a loop is the condition we set for it, when these conditions evaluate to false the loop will stop and the program will continue its execution right after it.
In some cases, we might need an extra condition, inside the loop, to stop its execution.
For these situations we can make use of the break instruction. This will stop immediately the execution of the loop and the program will continue its execution in the line right after the loop.
Another statement, slightly similar is the continue instruction. This one won’t stop the whole execution of the loop, instead, it will stop the current iteration of it and send the loop back to evaluate its conditions.
- Loops allow us to repeat the execution of a set of instructions while some conditions are true
- Loops are basically made of two parts, a condition or set of conditions to verify and a set of instructions to execute
- There are 3 types of loops: For Loops, While loops and do-while loops