Deutsch   English   

7. if-else (selection)

 

 

YOU LEARN HERE...

 

with the help of the if-else structure to execute certain program blocks only under certain conditions (selectively).

 

 

EXAMPLES

 

The selection is introduced with the keyword if, followed by a condition. The statements in the if block are only executed if the condition is true, otherwise the statements are executed after else, i.e. in the else block. The comparison operators >, >= , < , <= , == , != are usually used in the if condition. The statements in the if or else block must be indented. The else block can also be omitted.

Example 1: Changing the line color
If the starting point of a line has a negative x-coordinate, the line is drawn in red, otherwise in green.

Program:     

# Gp7a.py
from gpanel import *

makeGPanel(-30, 30, 0, 60)

x = -30
while x <= 30:
    if x <= 0:
        setColor("magenta")
    else:    
        setColor("cyan")
    line(0, 50, x, 0)
    x = x + 1
    delay(50)
► Copy to clipboard
 




Example 2
: Random rain
50,000 points are randomly selected in a square. If a point lies within the inscribed circle, it is drawn in red, otherwise in green. Since the radius of the circle is 100, the condition x2 + y2 < 1002 applies to the inner points. You define the drawing of a circle in the corresponding color at a randomly selected position in the randomDot() function.

Program:      

# Gp7b.py
from gpanel import *
from random import randint

def randomDot():
    x = randint(-100, 100)
    y = randint(-100, 100)
    if x * x + y * y < 10000:
        setColor("red")
    else:
        setColor("green")
    pos(x, y)            
    fillCircle(0.5)
    
makeGPanel(-100, 100, -100, 100)

i = 0
while i < 50000:
    randomDot()
    i = i + 1
► Copy to clipboard
 

 

Example 3: Multiple selection
You can use the if-elif-else structure to check several conditions in succession. If the condition after if does not apply, the conditions after elif are checked one after the other. If none of the conditions are met, the statements after else are executed. A colon after each condition and the correct indentation are important.
In the example, the colors of the small circles are chosen according to the size of their x-coordinates.

Program:      

# Gp7c.py
from gpanel import *

makeGPanel(0, 20, 0, 20)

x = 1
while x < 20:
    y = 1
    while y < 20:
        pos(x, y)
        if x < 5:
            setColor("red")
        elif x < 10:
            setColor("yellow")
        elif x < 15:
            setColor("green") 
        else:
            setColor("magenta")           
        fillCircle(0.5)
        y = y + 1
    x = x + 1
► Copy to clipboard
 

 

Example 4: Linking conditions with and
The condition combined in this way is only true if both partial conditions are true. Here, red dots are only drawn if the x-coordinate of the turtle is greater than 5 and less than 15.

Program:     

# Gp7d.py
from gpanel import *

makeGPanel(0, 20, 0, 20)

x = 1
while x < 20:
    y = 1
    while y < 20:
        pos(x, y)
        if x > 5 and x < 15:
            setColor("red")       
        else:
            setColor("yellow")           
        fillCircle(0.5)
        y = y + 1
    x = x + 1
► Copy to clipboard
 

 

 

REMEMBER YOU...

 

If the condition after if is true, the following program block is executed, otherwise the program block after else is executed. If the else block is missing, the program continues with the next line. For a multiple selection, use the in-elif-else structure.

The conditions can be linked with and or, whereby both conditions must be fulfilled for and and at least one of the two conditions must be fulfilled for or.

 

 

TO SOLVE BY YOURSELF

 

1)


Create the adjacent graphic using the if-else structure.

 


 

2)


Draw 1000 lines with a starting point in the middle of the window and end points at points with random coordinates in the range -100 to 100. The lines with a positive x-coordinate are colored red, the others green.

 
 

3)


Draw the adjacent figure using the if-else structure