Loops and conditional statements can be nested
for
-loops can be nested within conditional statements, and conditional statements can be nested within loops.
for
-loops within conditional statements
Here is the structure of a for
-loop nested within an if
-statement:
if <condition>:
for i in range(...):
# indented code block
In this case, the loop executes if the condition evaluates to True
, otherwise it is skipped.
Another possibility is for
-loops nested within an if-else
statement:
if <condition>:
for i in range(...):
# indented code block
else <condition>:
for i in range(...):
# indented code block
Examples: Trace the following code examples, confirming the output.
Code | Output | Notes |
|
|
The condition |
|
|
The condition |
Conditional statements within for
-loops
Here is the structure of an if
statement nested within a for
-loop:
for i in range(...):
if <condition>:
# indented code block
In this case, the condition is checked newly on each iteration of the loop. In some of the iterations it may evaluate True
, and then in others it may be False
. The check is done independently each time. The indented code block only executes on the iterations where the condition comes out to be True
.
Of course, inside the for
-loop we may instead have an if-else
statement or an if-else
ladder.
Examples: trace the following code examples, confirming the output.
Code | Output | Notes |
|
|
Each time through the loop, the variable |
Putting it all together
Here's a complete program that uses an if-else
statement nested within a for
-loop. Carefully trace the program and predict the image that results. You can run the code to test if you were correct.
"""
Produce a drawing of randomly-placed circles in two colors
Mystery output: Trace the code to find out. Run it to check.
Author: COMP 1351 Instructor
Date:
File: two_colors.py
Course: COMP 1351
Assignment: Preview assignment for for-loops
Collaborators: 1351 Instructors
Internet Sources: None
"""
import dudraw
from random import random
dudraw.set_canvas_size(500, 500)
dudraw.clear(dudraw.LIGHT_GRAY)
for i in range(10000):
# generate random x and y locations:
x = random()
y = random()
# set color based on position
if y > 0.5:
# Magenta is a hot-pink color
dudraw.set_pen_color(dudraw.MAGENTA)
else:
# Cyan is a turquoise color
dudraw.set_pen_color(dudraw.CYAN)
# draw the circle at the randomly-chosen location:
dudraw.filled_circle(x, y, 0.01)
# outline the circle with a black edge:
dudraw.set_pen_color(dudraw.BLACK)
dudraw.circle(x, y, 0.01)
# display the final image until the window is closed
dudraw.show(float('inf'))