Predict the output of the following code
fragments:
(a) count = 0
while count < 10:
print ("Hello")
count += 1
(b) x = 10
y=0
while x >y:
print x, y
X = X - 1
y = y + 1
Answers
Answer:
(a) hello hello hello hello hello hello hello hello hello hello
(b) 10 0 9 1 8 2 7 3 6 4
Explanation:
(a) The while loop will first check the condition i. e whether 'count' which contains the value 0 is true or not but the condition is true so it will print "hello " and will increase the value of count by one. The loop will continue displaying "hello" until the condition gets false, which will happen when the value of count becomes 10. So it will run for 10 times from 0 to 9.
(b) The loop will check the condition and as it true so first it will print the value of x and y and then decrease the value of x and increase the value of y and will continue the loop till value of x and y becomes 5 now the condition become false and hence loop terminates.