Preview

07 - Nested Loops

 1. A nested loop is:

  where one loop is placed inside a variable

  where one loop is placed inside another loop.

  None of the above

  where two loops are placed inside a single statement

 2. On what lines are the two loops in the following program and what does it do?
i = 2
while(i < 100):
   j = 2
   while(j <= (i/j)):
      if not(i%j): break
      j = j + 1
   if (j > i/j) : print i, " is prime"
   i = i + 1

print "Good bye!"

  Lines 2 and 4 - the program finds the prime numbers between 2 and 100

  Lines 2 and 4 - the program calculates the two times table up to 100

  Line 1 and 3 - the program finds the prime numbers between 2 and 100

  Lines 1 and 3 - the program calculates the two times table up to 100

 3. The structure of a nested loop is shown below. In what situations is a nested loop necessary?
for [first iterating variable] in [outer loop]: # Outer loop
    [do something]  # Optional
    for [second iterating variable] in [nested loop]:   # Nested loop
        [do something] 

  When it is necessary to iterate through one loop and use an if statement on another

  None of the above

  In game design where you are facing a decision (e.g. while health > 0 do not end game)

  When it is necessary to iterate through two different lists to solve the problem (e.g. to process a 2d array)

 4. Here we have two lists and a nested loop. What is the output of this program?
num_list = [1, 2, 3]
alpha_list = ['a', 'b', 'c']

for number in num_list:
    print(number)
    for letter in alpha_list:
        print(letter)

  1a2a3a1b2b3b1c2c3c

  1a2b3c

  1abc2abc3abc

  123abc

 5. The following nested loop produces an output of 123abc
num_list = [1, 2, 3]
alpha_list = ['a', 'b', 'c']

for number in num_list:
    print(number)
for letter in alpha_list:
    print(letter)

  Incorrect - it produces an output of abc123

  It is a nested loop but does not produce an output at all - an error occurs

  Correct

  It does produce an output of 123abc but it is not nested

 6. There are three programs here using for loops. What are the three outputs from each?
# What is the value of x?
x = 0
for i in range(3):
    x = x + 1
print(x)
 

# What is the value of x?
x = 0
for i in range(3):
    x = x + 1
for j in range(3):
    x = x + 1
print(x)



# What is the value of a?
x = 0
for i in range(3):
    x = x + 1
    for j in range(3):
        x = x + 1
print(x)

  2,4,6

  1,3,6

  3,8,15

  3,6,12

 7. The following nested loop will not print out each word in the list but each letter of every word in the list one after each other
listofpets=["goose","moose","dog","pigeon"]

#***NESTED LOOP***************
for i in listofpets:
        for x in i:
            print(x)

  FALSE

  TRUE

 8. What is the output of the following nested loop? (first two lines)?
for x in range(3):
    for y in range(3):
        print (x,y)

  (0,1) (0,1)

  (2,0) (0,1)

  (1,0) (1,1)

  (0,0) (0,1)

 9. The following code produces the 1 and 2 times table. What do you need to change to make it produce the 3 and 4 times table as well?
for x in range(1,3):
    for y in range(1, 11):
        print ('%d * %d = %d' % (x, y, x*y))

  Change the first line to: for x in range(4,5):

  Change the second line to: for y in range(4,5)

  Change the first line to: for x in range(1,5):

  Delete the first line and change the second line to: for y in range(4,5)

 10. For loops can be nested inside each other. There is a restriction to just '2' loops for the count of the inner loop

  TRUE

  FALSE