Q. What will be the outputs produced by following code fragments? First determine output on paper and then verify by running on computer.


 
Program Code Dry run (determine output on paper) Actual output code and find (run out)
(a) a = 8
while (a > 0):
    print ("Gotcha!")
    a = a - 3
else:
    print ("Going out!")
Gotcha!
Gotcha!
Gotcha!
Going out!
Gotcha!
Gotcha!
Gotcha!
Going out!
>>>
(b) a = 8
while (a > 0):
    print ("Gotcha!")
else:
    print ("Going out!")
Here update expression is not defined. So this while loop run infinite times. Here update expression is not defined. So this while loop run infinite times.
(c) a = 8
while (a > 0):
    print ("Gotcha!")
    a = a + 3
else:
    print ("Going out!")
Here condition defined while loop is always True that is while loop will not stop. Here condition defined while loop is always True that is while loop will not stop.
(d) a = 8
while (a > 0):
    print ("Gotcha!")
    a = a - 3
    if a == 5:
    break
else:
    print ("Going out!")
Gotcha! Gotcha!
>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post