HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
Deutsch   English   

8. for-loop

 

 

YOU LEARN HERE...

 

how to use for loops as an alternative to while loops or repeat loops.

 

 

EXAMPLES

 

In many programming languages, for loops are a frequently used alternative to while loops and are used in particular when a loop variable is changed by the same value (in Python, an integer value) with each loop pass.

The simplest for loops are so-called counting loops in the form

for i in range(n):
    Anweisung1 
    Anweisung2 
    ...        

range(n) returns the numbers 0, 1, 2, ... to n-1, i.e. a total of n numbers (it is actually a list with the numbers [0, 1, ..., n-1]). The start value of i is 0 and i is increased by 1 after each loop pass. The instructions in the loop block are therefore repeated n times. This loop corresponds to a while loop with a start value of 0, the loop condition i < n and the value change i = i +1.

Note: If i is not used in the loop body, a repeat loop can be used in TigerJython instead of the for loop, which does not require any variables and is therefore easier to understand for programming beginners.

Example 1: Drawing a set of lines with a for loop
21 lines are drawn from left to right and 21 lines from right to left. The variable i passes through the values 0, 1, 2, ... 20, i.e. a total of 21 values.

Program:     

# Gp8a.py
from gpanel import *

makeGPanel(0, 20, 0, 20)

for i in range(21):
    line(0, 10, 20, i)
    line(20, 10, 0, i)
    delay(100)
► Copy to clipboard
 




Example 2
: Using for loops with 1, 2 or 3 parameters

The general form of a for loop uses range() with three parameters:

for i in range(start, stop, step):
    Anweisung1
    Anweisung2
    ...          

The start value start does not have to be 0 at the beginning and the value change step can be any integer (even negative). If step is positive, the instructions in the loop block are repeated as long as i is less than the stop value. The difference between the for loops with 1, 2 or 3 parameters can be clearly seen in the following examples:


for
i in range(60): rectangle(i, i)
  
for
i in range(20,40): rectangle(i, i)
  
for
i in range(10,40,3): rectangle(i, i)

 

Example 3: Using nested for loops
To draw a chessboard, you go through all the fields of an 8x8 matrix with a row index x and a column index y using two nested for loops. Start at y = 0 and go through all x with a fixed value of y in the inner loop, then set y = 1, etc.

Program:      

# Gp8c.py
from gpanel import *

makeGPanel(0, 8, 0, 8)
setColor("blue")

for y in range(8):
    for x in range(8):
        if (x + y) % 2 == 0:
            fillRectangle(x, y, x + 1, y + 1)
► Copy to clipboard
 

if (x + y) % 2 == 0 checks whether the sum of the row and column index is an even number. As you can easily see, a filled square must be drawn in this case. The modulo division a % b returns the remainder of the division when a is divided by b as an integer. a % 2 therefore returns 0 if a is even and returns 1 if a is odd.

Example 4: Drawing a moiré pattern
A moiré is created when straight lines overlap. You can create the adjacent figure with two nested for loops.

Program:     

# Gp8d.py

from gpanel import *

makeGPanel(0, 10, 0, 10)

for i in range(0, 11):
    for k in range (0, 11):
        line(i, 0, k, 10)
        delay(100)
    
for i in range(0, 11):
    for k in range (0, 11):
        line(0, i, 10, k)
        delay(100)
► Copy to clipboard
 

 

 

REMEMBER YOU...

 

The range() function in the for loop can have 1, 2 or 3 parameters (start, stop, step). If there is only one parameter, range(n) returns the numbers 0, 1, 2, ...to n-1, so the commands in the loop are repeated n times (same as with repeat n:)

Instead of a for loop, you can always use a while loop. The reverse is not the case, as only integers are permitted for the value change in the for loop.

 

 

TO SOLVE BY YOURSELF

 

1)


Draw 10 squares with a for loop.





 


a) In a row

b) In a diagonal

 

2)


Experiment with sets of lines and create some nice graphics.





 






 

3)


Go through all the fields of an 8x8 matrix with nested for loops and color them according to the adjacent template.