# Implied DO

Implied DO-loops provide a fast way of listing many items. These items, depending on the place where an implied DO is used, can be variables, expressions, or even implied DO-loops.

### Syntax and Semantics

The syntax of an implied DO is the following:
( item-1, item-2, ...., item-n, DO-var = initial, final, step )

( item-1, item-2, ...., item-n, DO-var = initial, final )
It starts with a (, followed by a set of items, separated by commas, followed by a DO variable, an equal sign, an initial value, a final value, and a step-size, end ends with a ). Like a typical DO-loop, if the step size is 1, it can be eliminated.

Depending on the place where an implied DO is used, the items can be variables, including array elements, or expressions.

The meaning of an implied DO is simple: For each possible value of the DO variable, all items (i.e., item-1, item-2, ..., item-n) are listed once and adjacent items are separated by commas.

### Examples

• In the following, the DO variable i can have values -1, 0, 1, and 2. The item to be listed is i.
( i, i = -1, 2 )

For i=-1, the item listed is -1. For i=0, the item listed is 0. For i=1, the item listed is 1. Finally, for i=2, the item listed is 2. Combining these four cases together, the items listed by the given implied DO are
-1,  0,  1,  2

• In the following, the DO variable i can have values 1, 4, 7 and 10. The items are i and i*i.
( i, i*i, i = 1, 10, 3 )

For i=1, the items listed are 1 and 1=1*1. For i=4, the items listed are 4 and 16. For i=7, the items listed are 7 and 49. Finally, for i=10, the items listed are 10 and 100. Combining these four cases together, the items listed by the given implied DO are
1,  1,  4,  16,  7,  49,  10,  100

• In the following, the DO variable is i and the items are a(i) and b(i+1).
( a(i), b(i+1), i = 1, 3 )

For i=1, the listed items are a(1) and b(2). For i=2, the listed items are a(2) and b(3). For i=3, the listed items are a(3) and b(4). In summary, there are six items listed as i goes from 1 to 3:
a(1),   b(2),   a(2),   b(3),   a(3),   b(4)

• The following implied DO has three items and the DO variable k runs from 3 to -3 with a step size -3.
( k*a(k), b(k)-c(k-1), k, k = 3, -3, -3 )

For k=3, the listed items are 3*a(3), b(3)-c(2) and 3. For k=0, the listed items are 0*a(0), b(0)-c(-1) and 0. For k=-3, the listed items are (-3)*a(-3), b(-3)-c(-4) and -3. Therefore, there are nine listed items:
3*a(3), b(3)-c(2), 3, 0*a(0), b(0)-c(-1), 0, (-3)*a(-3), b(-3)-c(-4), -3

### Nested Implied DOs

An implied DO can be an item of another implied DO. For a nested implied DO, it would be better to work from outside-in and treat an inner implied DO as an item. The following examples illustrate this idea.
• The following implied DO contains an inner implied DO.
( i, ( i*j, j = 1, 3), i = 1, 3)

For i=1, the listed items are 1 and (1*j, j=1,3). For i=2, the listed items are 2 and (2*j, i=1,3). For i=3, the listed items are 3 and (3*j, j=1,3). Thus, without expanding the inner implied DO-loops, the listed items are
1,  (1*j, j=1,3),  2,  (2*j, j=1,3),  3,  (3*j, j=1,3)

Since (1*j, j=1,3) would generate 1*1, 1*2 and 1*3, (2*j, j=1,3) would generate 2*1, 2*2 and 2*3, and (3*j, j=1,3) would generate 3*1, 3*2 and 3*3, the given nested implied DO-loop generates 12 items as listed below:
1,  1*1,  1*2,  1*3,  2,  2*1,  2*2,  2*3,  3,  3*1,  3*2,  3*3

• The outer implied DO has a DO variable i running from 1 to 3. The only item in the outer DO is (a(i)*b(j), j=1, 2).
((a(i)*b(j), j=1, 2), i=1, 3)

For i=1, the listed item is (a(1)*b(j),j=1,2). For i=2, the listed item is (a(2)*b(j),j=1,2). For i=3, the listed item is (a(3)*b(j),j=1,2). Therefore, after unrolling the outer implied DO, we have:
(a(1)*b(j),j=1,2),  (a(2)*b(j),j=1,2),  (a(3)*b(j),j=1,2)

There is no nested implied DOs in the above. Since j runs from 1 to 2, expanding these three loops yields
a(1)*b(1),  a(1)*b(2),  a(2)*b(1),  a(2)*b(2),  a(3)*b(1),  a(3)*b(2)

• The inner and outer loops are not interchangeable.
((a(i)*b(j), i=1, 3), j=1, 2)

The above is almost identical to the previous one, except that i=1,3 and j=1,2 change their positions. The result is:
a(1)*b(1),  a(2)*b(1),  a(3)*b(1),  a(1)*b(2),  a(2)*b(2),  a(3)*b(2)

This list is different from the one in the previous example.