A DO loop is a special statement that constructs an iterative loop (a loop that is repeated a specified number of times).

- n = Statement number to identify end of the loop
- I = Index or control variable
- INITIAL = Initial value given to control variable
- LIMIT = Limiting value of control variable
- STEP = Increment added to control variable each loop repetition
- Control variable assigned initial value.
- Check if value of control variable exceeds the limiting value; if it does, do NOT execute loop.
- Execute body of loop.
- Change value of control variable by adding the value of STEP.
- Repeat steps 2 and 3.
- INITIAL, LIMIT, and STEP can be:
- positive or negative,
- integer or real,
- constants, variables, or expressions.
- Do not change the value of I, the control variable, inside the loop.
- Changing the value of INITIAL, LIMIT, or STEP inside the loop has no effect on the number of times the loop is executed.
- The CONTINUE statement is strongly encouraged as the last statement in the loop, but most other executable statements are allowed. (See textbook for a specific list.)
- After the DO loop is completed, the control variable (I) contains the last value that exceeded the limit.
- You can transfer out of a DO loop before it is completed.
- You can only enter a DO loop through the DO statement.
- If a step size is not given, 1 is assumed.
One DO loop may be completely contained within another DO loop. The two DO loops must use different control variables.

There are a number of different ways to represent DO loops in a flowchart. Four ways to flowchart the following DO loop are shown:
DO 70 I = 1, 30, 2
SUM = SUM + 1
70 CONTINUE

Example 1 - Example 2 - Example 3 - Example 4
Example 1:
DO 100 NUMBER = 1,5
PRINT*, NUMBER
100 CONTINUE
Output:
1
2
3
4
5
Example 2: DO 100 NUMBER = 10,1,-2
PRINT*, NUMBER
100 CONTINUE
Output:
10
8
6
4
2
Example 3: DO 50 N = 10,1
50 PRINT*, N
50 CONTINUE
Output:
None - loop not executed
No comments:
Post a Comment
=(*_*)------------------------(^_^)=
:::::|berkomentar dengan sopan adalah akhlak kemulian|:::::