Code::Blocks simple guide – Repetitive calculations using do-loop

 


In certain cases we might want to create a code to perform a repetitive calculation i.e. to perform same set of calculations many times. This can be done using do-loop in Fortran. The structure of a do-loop looks like the following.


Let us take an example whereby we want to generate the position of an object, with a fixed increment in position and for a certain amount of times depending on the input given by user. Assuming that we just need to know the position of the object at the end of the iterative process, then we do not need to record the position of the object at each iteration. If the converse is true, then one needs to make use of arrays to store the information. This will be covered later. For now, it is suffice to just print the value of the position at each iteration on the terminal. 


In the example given above, the position ” x ” will be increased by a value of ” delta_x ” for a total of ” ite_max ” times. The results appearing on the terminal is shown below.


The full code for the example above can be found here.


Instead of an incremental loop, one may also opt for a decremental one by changing the first line of the do-loop such as:


The output for the decremental do-loop will be: