Unit 6 : Repetition Structure Flashcards
What is counter based looping?
- Known number of iteration
Examples of counter based looping ( 2 )
- While
- For
What is sentinel based looping ?
- Unknown number of iteration
Examples of sentinel based looping
- while
What is counter based loops also called?
- definite loops
What is the difference between break and continue?
- break - ends the loop and jumps to the statement
- continue - ends the current iteration and jumps to the top of the loop and starts the next iteration
How can we create a definite loop with string?
friends = [“Joseph”,”Glenn”,”Sally”]
for i in friends:
print(friends)
print(“Done”)
How can we sum inside a loop?
val = 0
print (“Before”, val)
for num in [9, 41, 12, 3, 74, 15] :
val = val + num
print (val, num)
print (“After”, val)
Before 0
9 9
50 41
62 12
65 3
139 74
154 15
After 154
How can we find the average inside a loop?
count = 0
sum = 0
print (“Before”, count, sum)
for value in [9, 41, 12, 3, 74, 15]:
count = count + 1
sum = sum + value
print (count, sum, value)
print (“After”, count, sum, sum / count)
How can we filter a loop?
print “Before”
for value in [9, 41, 12, 3, 74, 15]:
if value > 20:
print (“Large number”,value)
print (“After”)
How can we find using a boolean variable
found = False
print (“Before”, found)
for value in [9, 41, 12, 3, 74, 15]:
if value == 3 :
found = True
print (found, value)
print (“After”, found)
How can we find the smallest number?
smallest = None
print (“Before”)
for value in [9, 41, 12, 3, 74, 15]:
if smallest is None :
smallest = value
elif value < smallest :
smallest = value
print (smallest, value)
print (“After”, smallest)