Computer Science
Predict the output of the following code fragments:
keepgoing = True
x=100
while keepgoing :
print (x)
x = x - 10
if x < 50 :
keepgoing = False
Python
Python Control Flow
41 Likes
Answer
100
90
80
70
60
50
Working
Inside while loop, the line x = x - 10
is decreasing x by 10 so after 5 iterations of while loop x will become 40. When x becomes 40, the condition if x < 50
becomes true so keepgoing
is set to False
due to which the while loop stops iterating.
Answered By
13 Likes
Related Questions
Predict the output of the following code fragments:
count = 0 while count < 10: print ("Hello") count += 1
Predict the output of the following code fragments:
x = 10 y = 0 while x > y: print (x, y) x = x - 1 y = y + 1
Predict the output of the following code fragments:
x = 45 while x < 50 : print (x)
Predict the output of the following code fragments:
for x in [1,2,3,4,5]: print (x)