KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Predict the output of the following snippet:

p = -99
while(p < 0):
    p = p + 9
    print(p, end=' ')

Python Control Flow

1 Like

Answer

Output
-90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0 
Explanation

Let’s break down the code line by line:

1. p = -99: The variable p is assigned the value -99.

2. while(p < 0):: The while loop executes as long as the value of p is less than 0.

3. In each iteration, the statement p = p + 9 increases the value of p by 9 and print(p, end=' ') displays the updated value in the same line separated by spaces.

4. The values of p become -90, -81, -72, -63, -54, -45, -36, -27, -18, -9 and 0.

5. When p becomes 0, the condition p < 0 becomes False and the loop terminates.

6. Therefore, the output is -90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0.

Answered By

1 Like


Related Questions