KnowledgeBoat Logo

Computer Science

In the nested for loop code below, how many times is the condition of the if clause evaluated?

for i in range(4):
  for j in range(5):
      if i + 1 == j or j + 1 == 4:
         print ("+", end = ")
      else:
         print ("o", end = ")
print()

Python Control Flow

11 Likes

Answer

Outer loop executes 4 times. For each iteration of outer loop, inner loop executes 5 times. Therefore, the total number of times the condition of the if clause gets evaluated is 4 * 5 = 20.

Answered By

9 Likes


Related Questions