[go: up one dir, main page]

0% found this document useful (0 votes)
19 views23 pages

Implementing Loops Slides

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views23 pages

Implementing Loops Slides

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Implementing Loops

Mateo Prigl
Software Developer
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py

bool_var = True

while bool_var:
print("It is true!")
print("It is really true!")

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
It is really true!
bool_var = True

while bool_var:
print("It is true!")
print("It is really true!")

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
It is really true!
bool_var = True It is true!
It is really true!
while bool_var:
print("It is true!")
print("It is really true!")

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
It is really true!
bool_var = True It is true!
It is really true!
while bool_var: It is true!
print("It is true!") It is really true!
print("It is really true!") ...

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
bool_var = True Still true? (y/n):

while bool_var:
print("It is true!")
user_input = input("Still true? (y/n): ")
if user_input == "n":
bool_var = False

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
bool_var = True Still true? (y/n): y

while bool_var:
print("It is true!")
user_input = input("Still true? (y/n): ")
if user_input == "n":
bool_var = False

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
bool_var = True Still true? (y/n): y
It is true!
while bool_var: Still true? (y/n):
print("It is true!")
user_input = input("Still true? (y/n): ")
if user_input == "n":
bool_var = False

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
bool_var = True Still true? (y/n): y
It is true!
while bool_var: Still true? (y/n): y
print("It is true!")
user_input = input("Still true? (y/n): ")
if user_input == "n":
bool_var = False

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
bool_var = True Still true? (y/n): y
It is true!
while bool_var: Still true? (y/n): y
print("It is true!") It is true!
user_input = input("Still true? (y/n): ") Still true? (y/n):
if user_input == "n":
bool_var = False

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
bool_var = True Still true? (y/n): y
It is true!
while bool_var: Still true? (y/n): y
print("It is true!") It is true!
user_input = input("Still true? (y/n): ") Still true? (y/n): n
if user_input == "n": Moving on!
bool_var = False

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


It is true!
Still true? (y/n): y
user_input = "y" It is true!
Still true? (y/n): y
while user_input == "y": It is true!
print("It is true!") Still true? (y/n): n
user_input = input("Still true? (y/n): ") Moving on!

print("Moving on!")
Iterables

List Tuple Dictionary

Set String
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py

lst = [23, 45, 67]


i = 0

while i < len(lst):


print(f"{i} element is {lst[i]}")
i = i + 1

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


0 element is 23

lst = [23, 45, 67]


i = 0
0 3
True
while i < len(lst):
print(f"{i}0 element is {lst[i]}")
0

i = 0i + 1
1

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


0 element is 23
1 element is 45
lst = [23, 45, 67]
i = 0
1 3
True
while i < len(lst):
print(f"{i}
1 element is {lst[i]}")
1

i = i
1 + 1
2

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


0 element is 23
1 element is 45
lst = [23, 45, 67]
2 element is 67
i = 0
2 3
True
while i < len(lst):
print(f"{i}
2 element is {lst[i]}")
2

i = i
2 + 1
3

print("Moving on!")
Controlling the Program's Flow with a while Loop
The code block that belongs to the while loop is indented

script.py

> python3 script.py


0 element is 23
1 element is 45
lst = [23, 45, 67]
2 element is 67
i = 0
Moving on!
3 3
False
while i < len(lst):
print(f"{i} element is {lst[i]}")
i = i + 1

print("Moving on!")
Controlling the Program's Flow with a for Loop
The code block that belongs to the for loop is indented

script.py

> python3 script.py


23
45
67
lst = [23, 45, 67]

for num in lst:


print(num)

print("Moving on!")
Controlling the Program's Flow with a for Loop
The code block that belongs to the for loop is indented

script.py

> python3 script.py


0
1
2
3
for lc in range(5):
4
print(lc)
Moving on!

print("Moving on!")
Breaking out of the Loop
Loops with the else Clause

for/else while/else
Up Next:

Reusing Code with Functions

You might also like