Computer Science
Write the output of the following:
for x in range(10, 20):
if (x == 15):
break
print(x)
Answer
15
Working
The code sets up a for
loop that iterates over a range of numbers from 10 to 19. During each iteration, the variable x
takes on these values sequentially. Within the loop, an if
statement checks if x
is equal to 15. When x
reaches 15, the condition is met, and the break
statement is executed, immediately exiting the loop. As a result, the loop terminates prematurely, and the value of x
at the point of exit, which is 15, is printed.
Related Questions
Write the output of the following:
for j in range(10, 6, -2): print(j*2)
Write the output of the following:
for x in range(1, 6): for y in range(1, x+1): print (x, ' ', y)
Write the output of the following:
for x in range(10, 20): if (x % 2 == 0): continue print(x)
Write the output of the following program on execution if x = 50:
if x > 10: if x > 25: print("ok") if x > 60: print("good") elif x > 40: print("average") else: print("no output")