Preview

02 - Iteration: While and For Loops

 1. One of the benefits of a WHILE loop is that you can use it when you are unsure of how many iterations you wish to carry out.
Note: The following presentation provides an overview of WHILE loops

  FALSE

  TRUE

 2. In the following snippet of code, when will the last line: "print("Finito") be executed?
x = 1
while x < 5:
    print ("Test and Track")
    x = x + 1
print ('Finito')

  This line is outside of the while loop, so will be printed at the end (on exiting the loop)

  The output will print 'Test and Track' four times and then print 'Finito'.

  All of the above statements are correct

  The last line would also be executed IF the while loop's starting condition was not met.

 3. A disadvantage of FOR loops is that you can only use them when you are not aware of how many iterations you need at the start.
Note: The following presentation presents an overview of FOR loops

  TRUE

  FALSE

 4. There are two functions here - what does each of them do?
def main():
   using_a_while_loop()
   using_a_for_loop()

def  using_a_while_loop():
   print("Using a While Loop")
   i=1
   while i < 11:
      number=2
      print(i*number)
      i =i+1
      
#This uses a for loop   
def  using_a_for_loop():
   print("Using a For Loop to do the same thing")
  
   for i in range(1,11):
      number=2
      print(i*number)
      #i =i+1 #this is not needed as the for loop automatically goes up in the range

main()

  Both loops check to see if the number 1 is a prime number

  Both loops produce the same output: printing out numbers 2,4,6,8,10,12,14,16,18,20

  The first one produces an output of 1 to 11 and the second produces an output of 2 to 12

  Both loops produce the same output: printing out numbers from 2 to 11 (2,3,4,5,6,7,8,9,10,11)

 5. Analyse the following code that utilises a WHILE loops. What does it do?
def login():
          tries=1
          while tries <4:
                    username="username1"
                    password="open123"
                    print('*********************************************')
                    print('Enter username')
                    answer1=input()
                    print('Enter password:')
                    answer2=input()
                    if answer1 == username and answer2 == password:
                          print("Access Granted")
                    else:
                          print("Sorry, Access Denied")
                          print("attempt no:")
                          print(tries)
                          tries += 1
                    if tries==4:
                              print("Sorry you have had exceeded the number of tries")
login()

  It allows the user to attempt to login (with the right combination of username/password) three times

  It allows the user to attempt to login (with the right combination of username/password) four time

  It is a progam that allows the user to enter incorrect data just once, and accepts '4' as a password.

  It allows the user to attempt to login (with the right combination of username/password) two times

 6. What happens if the user presses 'y'?
#Keep pressing enter to test this program
while 1:
    print ("Boo - you are in a loop")
    answer = input('Press y to end this loop')
    if answer == 'y':
        print ("Okay - I will stop")
        break
print ('Have a ')
print ('nice day!')

  It will just continue to print "Okay - I will stop" infinite times until you press ENTER

  It will print "print "Okay - I will stop" and then break out (or exit) the loop

  It will print "Okay - I will stop" and then print the word 'break'

  None of the above

 7. In the following code, what does line 1 mean?
while 1:
    print ('The meaning of life......')
    answer = input('Press y for the meaning of life: ')
    if answer == 'y':
        print ('Ahh.....the search has begun: ')
        continue #use of the continue statement
    answer = input('Had enough yet? ')
    if answer == 'y':
        break
print ('Have a ')
print ('nice day!')

  The body of the while loop would run, because the condition is met (it is True or 1)

  It is a way of ensuring that the loop always runs (because the condition is always met).

  All of the above statements are correct

  It is similar (the same would be achieved) as using While True:

 8. What is the output of the following code that uses a for loop?
#set a flag to false
found=False #this is the initial assumption

for x in range(0,11):
    #if x is equal to 5 then set the found flag to True and exit the loop!
    if x == 5:
        found=True
        break #this exits the loop
    else: #if 5 is not found, then continue and print all values for x
        print(x)

  0,1,2,3

  0,5,10

  0,1,2,3,4

  0,1,2,3,4,5,6,7,8,9,10,11

 9. In the following code that uses a for loop, what are the first three numbers that are output if a =1; b=1
a = 
b = 
for c in range(1,10):
    n = a + b
    a = b
    print(n)
    b = n

  2,3,4

  2,3,5

  01,2

  1,1,1

 10. Can you predict the output of the following code?
# What is the value of x - what is output?
x = 0
for i in range(3):
    x = x + 1
for j in range(3):
    x = x + 1
print(x)

  2

  4

  6

  1

 11. What is the output of the following code?
def main():
  for i in range(1,10,5):
    print(i)
main()

  10,5,1

  1,5,10

  1,6

  1,2,3,4,5

 12. The following code prints out 1,3,5,7,9. What would you change in line 2 to make it print 9,7,5,3,1?
def main():
  for i in range(1,10,2):
    print(i)
main()

  for i in range(10,1,-2):

  for i in range(9,1,2):

  All of the above statements are correct

  for i in range(9,-1,-2):

 13. What do you need to change for this code to print numbers from 1 to 10? (instead of 1 to 9)
def main():
  for i in range(1,10,1):
    print(i)
main()

  Change the first 1 to a 11

  Change the 10 to a 9

  Change the print(i) to print(10)

  Change the 10 to a 11

 14. ___________________ loops are also called WHILE loops or WHILE-ENDWHILE statements

  loop-controlled

  Condition-controlled

  Count-controlled

  print-controlled

 15. A ________________loop (FOR loop) iterates the number of times specified in the loop

  loop-controlled

  print-controlled

  Count-controlled

  Condition-controlled

 16. An infinite loop can occur with all condition-controlled loops but not with a count-controlled loop.

  TRUE

  FALSE

 17. With a REPEAT UNTIL condition-controlled loop, the code in the loop is executed at least once.
We do not have a loop that goes by this name in Python, but it is worth knowing about!

  TRUE

  FALSE

 18. Iteration is the process of __________________________ to achieve a particular target or goal

  deleting sections of a program

  repeating sections of a program

  duplicating variables in the code

  multiplying rather than adding

 19. Infinite loops …

  repeat four times + 1

  repeat forever

  repeat 100,000 times

  Do not repeat

 20. condition-controlled loops - repeat until ….

   something happens or a condition is met / not met

  None of the above

  a set number is reached (the number is always 10)

  until x = 1 (fixed condition in these sort of loops)