For Loop

The For loop is another repetition structure. The advantage of using the For loop is that the structure itself keeps track of incrimenting the counter, so you do not have to. Usually by default, the counter is just incrimented by 1 each time you go through the loop. As normal, there is some way to exit the loop, usually by some kind of Boolean expression. Some For loops allow you to just specify how many times you would like to do the loop, by providing a number and no Boolean expression.

To use the For…Next loop, there will also be a loop counter that keeps track of how many times through the loop the program has executed. Each time the code inside the loop is executed, the loop counter is automatically incremented by 1. Then an expression checks to see that the predetermined number of times has been reached.

The For loop (in most computer programming languages) takes the generic form of:

FOR counter in range(n)
statement(s)
ENDFOR

or

FOR (int counter = 0; boolean expresion; counter++)
statement(s)
ENDFOR

In this example program, the user is asked to enter a posative integer and the program will count how many times it goes through the loop until it reaches that number.

Top-Down Design for the For loop

Top-Down Design for While loop

Flowchart for the For loop

For loop flowchart

Pseudocode for the For loop

GET positive_integer
FOR (int counter = 0; counter < positive_integer; counter++)
SHOW counter
ENDFOR

Code for the For loop

Example Output

Code example output