This post will talk about the for loop within a program, as requested by the mastery 20. 

The for loop is used for the same task as the while loop, but its difference radicates in their notation. The notation of the for structure includes an inicialization, an accumulator and the condition; which difffers from the while loop which only asks for the condition. The structure for  the for loop is as follows:

for (initialization; condition; accumulator){

Instructions;

}

First of all, the initialization could be filled with the initialization of a value (int a= 0), a value previously initialized (it could be left blank if the initialization has been done before). Afterwards, the condition should return a boolean value, which could be done by a comparison (==, !=,) or by explicitely typing it as a true or false, which might create an infinite loop or a non used code, respectively. Finally the accumulator would be the increase or decrease of the value initialized, which could be a++ or a–. If the accumulation is done within the instructions, it could be left blank. 

Firstly, the initialization is done, afterwards the condition is tested. If false, the for ends and further instructions are executed. If true, the instructions within the loop are executed once, the accumulation is done and the condition is tested again for further execution.

In case you wish to stop the flow of a loop or avoid an infinite loop, a break command would be useful. A break stops the flow of the loop and jumps to execute the following instructions.

Here’s an image of a code for a succesion of numbers with a for loop and a break which stops the succesion:

 

CC BY 4.0 Loops 2.0 #Mastery20 by Carlos Gallegos is licensed under a Creative Commons Attribution 4.0 International License.