120 likes | 193 Views
Lab #2-4 Follow-Up: Further into Python. Part 2: Loops. Repetition: The Essence of Computing (Revisited). Recall our assembly-lanuage program for repeated addition: We would like a more abstract / expressive way of doing repetitions!. LOAD A R1 LOAD B R2 LOAD ZERO R3
E N D
Repetition: The Essence of Computing (Revisited) • Recall our assembly-lanuage program for repeated addition: • We would like a more abstract / expressive way of doing repetitions! LOAD A R1 LOAD B R2 LOAD ZERO R3 ADD R2 R3 DECR R1 JNZ R1 103 STOR C R3
for loops: When you know the # of reps for i in range(10): dance()
for loops: When you know the # of reps for i in range(10): dance() Like function
Can have more than one instruction in a loop for i in range(10): dance() speak('Oops I did it again!')
range creates a list of values to “loop over” for i in range(10): print(i)
range creates a list of values to “loop over” for i in range(10): print(i) 0 1 2 3 4 5 6 7 8 9 Output
range creates a list of values to “loop over” for i in range(10): print(i) 0 1 2 3 4 5 6 7 8 9 Output Google dijkstra archive zero for why!
forwithout range for name in ['Chris', 'Hannah', 'Will']: print(name) An explicit list
while: when you don't know how many reps # Get current time in seconds startTime = currentTime() # Keep moving for 30 seconds while (currentTime() – startTime) < 30: keepMoving()
while: when you don't know how many reps # Get current time in seconds startTime = currentTime() # Keep moving for 30 seconds while (currentTime() – startTime) < 30: keepMoving() A Booleancondition: either False or True