Python Tutorials (back to the list of tutorials)

Simple Repetitions

For Loop

The most frequently used logic for control flow is a for loop. Typically you put one integer variable as counter with the starting number zero ( int i=0 ), specify the total number ( i<10 ), and put increment of the counter ( i++ ) inside the "for" statement. Then the body of the loop is put between parentheses { ... }.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
IPoint(i, 0, 0)

```

The code above is equivalent to this code.

```add_library('igeo')

size( 480, 360, IG.GL )

IPoint(0,0,0) # 1st execution. i=0
IPoint(1,0,0) # 2nd execution. i=1
IPoint(2,0,0) # 3rd execution. i=2
IPoint(3,0,0) # 4th execution. i=3
IPoint(4,0,0) # 5th execution. i=4
IPoint(5,0,0) # 6th execution. i=5
IPoint(6,0,0) # 7th execution. i=6
IPoint(7,0,0) # 8th execution. i=7
IPoint(8,0,0) # 9th execution. i=8
IPoint(9,0,0) #10th execution. i=9
```

Please note that the last point is at (9, 0, 0), not (10, 0, 0) but total number of points is still ten because it started with zero.

This diagram on the right shows the flow of the execution.

1. int i=0 is executed once in the beginning.
2. The condition i<0 is checked if it's true or false.
3. If the condition is true, go to next step of 5. If it's false, it exits this for loop.
4. The body of for loop inside the parentheses { ... } is executed.
5. i++ is executed and go buck to 2.

Logics inside Body

The example code above just shows the repetition of ten times. Formation of geometries created in a repetition is up to the way you use the counting number i to create geometries. This is a fundamental part of design of an algorithm.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
IPoint(0, 0, i*5)

```

Instead of shifting points in X direction, this code shifts points in Z direction in the increment of 5.

This code below sets the color of points according to the counting number i.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
IPoint(0, 0, i*5).clr(i*0.1,0,0)

```

Another example code of putting the counter i into parameters of geometries.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
IPoint(i*10-50, 10-i*5, i*4).clr(i*0.1, 1.0-i*0.1, 0)

```

To understand the behavior of those parameters, let's see the equivalent unrolled code below.

```add_library('igeo')

size( 480, 360, IG.GL )

IPoint(-50,  10,  0).clr(  0, 1.0, 0) # i=0
IPoint(-40,   5,  4).clr(0.1, 0.9, 0) # i=1
IPoint(-30,   0,  8).clr(0.2, 0.8, 0) # i=2
IPoint(-20,  -5, 12).clr(0.3, 0.7, 0) # i=3
IPoint(-10, -10, 16).clr(0.4, 0.6, 0) # i=4
IPoint(  0, -15, 20).clr(0.5, 0.5, 0) # i=5
IPoint( 10, -20, 24).clr(0.6, 0.4, 0) # i=6
IPoint( 20, -25, 28).clr(0.7, 0.3, 0) # i=7
IPoint( 30, -30, 32).clr(0.8, 0.2, 0) # i=8
IPoint( 40, -35, 36).clr(0.9, 0.1, 0) # i=9
```

Yet another example code of putting the counter i into parameters of geometries using trigonometric functions.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
IPoint(sin(i*0.5)*20, \
cos(i*0.5)*20, \
i*10-50).hsb(i*0.1,1,1)
```

Nested Loop (2 Dimensional For Loop)

You can put for loop inside another for loop. You use two different counter variable i and j for each loop. In this way, you can have repetition in 2 dimensional matrix way.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
for j in range(10):
IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0)
```

Nested Loop (3 Dimensional For Loop)

In the same way of 2 dimensional nested for loop, you can have 3 dimensional nested for loop with three for loops. Now you use three different counter variable i, j, k.

```add_library('igeo')

size( 480, 360, IG.GL )

for i in range(10):
for j in range(10):
for k in range(10):
IPoint(i*10, j*10, k*10).clr(i*0.1, j*0.1, k*0.1)

```

(back to the list of tutorials)