A loop that executes when the number of iteration is explicit in the problem. The printing of numbers 1 to 10 is an example of this type of loop.
Problem: Print the first five even numbers.
We know that the first five even numbers are the numbers 2, 6, 4, 8, 10. The problem simply says that we need to print these even numbers. If we look closer to the numbers, we will notice that there is a step of 2 from every other number. To take this step in the program, the counter function should use 2 as incrementing step to produce the even numbers.
The solution shown is the same solution in printing the numbers 1 to 10 or 1 to 1000. The things new in this solution are the incrementing step used, and the condition set to terminate the loop.
The terminating condition not only checks if the curent value is less than 10 but also cheks if they are the same. The flowchart terminates, if the current value of ctr greater than 10.
Note that the incrementing step set in the counter function is not limited to 1. The counter step differs as the problem calls for it.
A loop that runs without end. It is due to mistakes in the logical operation or expression or variables used to control the termination of the loop. Others call this logical error. Often this type of error is the hardest type of error to debug.
Learning Example : Print the first 10 counting numbers
We know that the solution is simply to print numbers 1 to 10. If we made use of wrong variables and or logical expression, then we might end up in having infinite loop.
The following flow chart is a classic example of infinite loop that should be avoided.
Take note of what happened if the programmer accidentally initialized variable to 0 instead of 10.
Worst if the comparison used in the decision symbol to terminate the loop in satisfying the problem is also illogical ( wrong logical operations > instead of <, etc ). This will lead to infinite loops and can give us headache in identifying the problem. Simulation is the best way to debug this error.
Iteration - The repetition of the loop. When the given problem says that we have to print the numbers 1 to 10, this would mean that we will need a repetition of instruction PRINT 10 times. The number of repetitions the loop has taken to perform to satisfy the problem is the number of iterations.
Accumulator - A temporary storage location in a central processing unit that holds values during a process. Using the problem given in the iteration example, instead of printing the numbers 1 to 10, the problems looks at printing the sum of the numbers. Illustrating the problem we have,
Numbers given : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sum of the numbers : 1+2+3+4+5+6+7+8+9+10 = 55
This mean that we will ADD the numbers 10 times, at the same time there will be 10 iterations that will happen, but instead of printing the number as it increases from 1 to 10, we should take this increasing numbers, add them and store them in a temporary variable to hold the current sum value until we completed the 10 iterations.
The variable that temporarily holds this increasing value for sum is the accumulator. Take note that accumulator is used not only for addition but for multiplications and other mathematical applications as well.