Python While Loop:
|We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.|
(1) The while loop executes a block of code based on a condition. The loop continues to execute as long as the condition is true. The condition is a boolean expression. If the condition always remains true, then the while loop will never stop executing. When this condition becomes false, the statements in the body of the loop are not executed and the control is transferred to the statement immediately following the body of the while loop. If the condition of the while loop is initially false, the body is not executed even once. The statements within the body of the while loop must ensure that the condition eventually becomes false; otherwise, the loop will become an infinite loop, leading to a logical error in the program.
(2) Example of While Loop: Increment
(3) Example of While Loop: Decrement