Explanation:
- The variable num is initialized with the value 1.
- The while loop checks the condition num < 5. Since 1 < 5 is True, the loop executes.
- Inside the loop, print(num) prints the current value of num.
- However, there is no statement to increment num, meaning num always remains 1.
- Since 1 < 5 is always True, the loop never stops and results in an infinite loop.
Output (Infinite Loop):
(The loop will continue printing 1 forever.)
How to Fix It?
To ensure the loop terminates correctly, we should increment num inside the loop:
Correct Output:
Now, the loop stops when num becomes 5, as 5 < 5 is False.
0 Comments:
Post a Comment