Deutsch   English   

6. while-loop

 

 

YOU LEARN HERE...

 

how to deal with one of the most important program structures. The while loop can generally be used for any type of repetition and is found in practically all programming languages.

 

 

EXAMPLES

 

A while loop is introduced with the keyword while , followed by a condition and a colon. As long as the condition is fulfilled, the commands are repeated in the subsequent program block. The comparison operators < (smaller), <= (smaller-equal), > (larger) >= (larger-equal), == (equal), != (different) are usually used in the condition. A while loop usually requires a variable that is assigned a start value at the beginning and a value change after each loop pass.

Example 1: Drawing a series of points
Small red circles should appear in the diagonal of the graphics window. The first circle is drawn at position (1, 1), the second at (2, 2) and so on. In this example, a while loop is used with a loop counter i, which also determines the position of the circles As long as i is less than 20 (condition), the commands are repeated in the indented block, whereby i is incresed by 1 increased by 1 each time.

Program:     

# Gp6a.py
from gpanel import *

makeGPanel(0, 20, 0, 20)

setColor("red")
i = 1
while i < 20:
    pos(i, i)
    fillCircle(0.5)
    i = i + 1
► Copy to clipboard
 



Example 2
: Drawing a group of lines
You define the start and end points of the lines using the loop variable. This runs through the values from 0 to 40. To make it easier to observe the drawing, insert a short pause after each line with delay(100).

Program:     

# Gp6b.py
from gpanel import *

makeGPanel(0, 40, 0, 40)

i = 0
while i <= 40:
    line(i, 0, 40, i)
    delay(100)
    i = i + 1
► Copy to clipboard
 



Example 3
: Drawing a set of circles (filled circles with a black border)
The first circle has a radius of 20 and the radius of each subsequent circle is 1 smaller. You define a filled circle with a black border in the blueCircle(r) function.

Program:     

# Gp6c.py
from gpanel import *

def blueCircle(r):
    setColor("cyan")
    fillCircle(i)
    setColor("black")
    circle(r)
     
makeGPanel(-20, 20, 0, 40)

i = 20
while i > 0:
    pos(0, i)
    blueCircle(i)
    i = i - 1
    delay(100)
► Copy to clipboard
 



Example 4
: Using nested while loops
The variable x in the outer loop takes the values from 1 to 19. With each new x, the inner loop runs over all y (the column at position x is filled up). Make sure that the two program blocks are indented correctly.

Program:     

# Gp6d.py
from gpanel import *

makeGPanel(0, 20, 0, 20)

setColor("red")

x = 1
while x < 20:
    y = 1
    while y < x:
        pos(x, y)
        fillCircle(0.5)
        y = y + 1
    x = x + 1
► Copy to clipboard
 


 

 

 

REMEMBER YOU...

 

A while loop is introduced with the keyword while , followed by a condition and a colon. A while loop usually requires a variable that is assigned a start value at the beginning and a value change after each loop pass.

By using nested while loops make sure that the program blocks are indented correctly.

 

 

TO SOLVE BY YOURSELF

 

1)


Choose a suitable coordinate system and draw the adjacent set of lin

 


 

2)


Add the set of lines from example 2 to the adjacent picture.

 
 
3)


Change the program from example 4 so that the following graphics are created::