CMPE 102 - Module 6 - Iteration or Looping in Python (1)
CMPE 102 - Module 6 - Iteration or Looping in Python (1)
Iteration/Looping in Python
In this module you will learn how to rerun parts of
your program without duplicating instructions
1-1
Minus 21!
slide 3
END GAME
Re-running the entire
program Pseudo code
While the player wants to play
Run the game again
slide 4
…rest of
program
slide 5
slide 6
slide 7
No
slide 9
slide 10
slide 11
• Syntax:
(Simple condition)
while (Boolean expression):
body
(Compound condition)
while (Boolean expression) Boolean operator (Boolean expression):
body
slide 17
4) Update control
slide 18
slide 19
slide 20
slide 21
slide 22
total = 0
4) Update control
for i in range (1, 4, 1):
total = total + i
print("i=", i, "\ttotal=", total)
3) Execute body
print("Done!")
slide 28
slide 29
slide 30
slide 31
slide 32
• For: for_increment5.py
for i in range (0, 105, 5):
print("i =", i)
print("Done!")
slide 33
slide 37
slide 38
slide 39
slide 40
Y
…rest of program
slide 41
slide 42
slide 43
slide 44
slide 46
DEPARTMENT OF COMPUTER ENGINEERING
Prg C * *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
slide 47
DEPARTMENT OF COMPUTER ENGINEERING
Nested Loops
slide 49
• Example 2: infinite2.py
i = 10
while (i > 0):
print("i = ", i)
i = i + 1
print("Done!")
slide 50