Sixthweeknote
Sixthweeknote
Examples:
for i in range(5):
if i == 3: break
print(i) # prints 0, 1, 2
for i in range(5):
if i == 3: continue
print (i) # prints 0, 1, 2, 4
8.5 Nested Loops
A loop inside another loop.
Example:
for i in range(2):
for j in range(3):
print("i ="+ i+ "j ="+j)
The inner loop runs fully every time the outer
loop runs once.
8.6 Warning: Avoid Infinite Loops!
If a while loop never becomes False, it will
keep running forever.
Example of an infinite loop:
x=1
while x > 0:
print(x)
This loop never ends unless you stop the
program, because x keeps being more than 0.
How to fix:
Always make sure your loop has a condition
that will eventually become False, like:
x=1
while x < 5:
print(x)
x += 1
On Windows:
Alt + F4 – closes the active window or
program.