Computer Science
Predict the output of the following code fragments:
c = 0
for x in range(10):
for y in range(5):
c += 1
print (c)
Python
Python Control Flow
28 Likes
Answer
50
Working
Outer loop executes 10 times. For each iteration of outer loop, inner loop executes 5 times. Thus, the statement c += 1
is executed 10 * 5 = 50 times. c is incremented by 1 in each execution so final value of c becomes 50.
Answered By
13 Likes
Related Questions
Predict the output of the following code fragments:
for x in [1,2,3]: for y in [4, 5, 6]: print (x, y)
Predict the output of the following code fragments:
for x in range(3): for y in range(4): print (x, y, x + y)
What is the output of the following code?
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()
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()