Python While Loop
Introduction
As long as the condition specified in the while loop statement is true, a while loop statement in Python may be used to continually execute a specific statement. While loops provide the computer the ability to repeatedly go over a chunk of code.
Syntax of While Loop in Python:
while test_expression: |
The while loop condition is initially evaluated by the program. If it is, the program enters the while loop and runs its body. Otherwise, it exits. While the condition is true, it keeps running the while loop's body. When it is false, the program exits the loop and ceases to iterate over the while loop's body. Let's look at the next example to better comprehend it.
a = 1 |
Output:
loop entered 1 times |
Infinite While Loop
The term "infinite while loop" describes a while loop in which the while condition is permanently true. The program enters the loop and repeatedly executes the same block of code, never coming to a conclusion. This happens when a condition never turns false. A never-ending loop may be seen in the example below:
a = 1 |
The code block above will conduct an indefinite loop that repeatedly asks for our names if we run it. If we don't press "Ctrl+C," the cycle will continue.
Output:
Enter Name #Stopped the loop by entering CTRL+C File "", line 2, in |
Do While Loop
There isn't a do-while loop in Python. However, we can develop a do-while software. After the statement has been executed, it is used to check the conditions. Similar to a while loop, but it runs at least once.
i = 1 |
Output:
1 |
While True or 1
Declaring a condition to be true without evaluating any expressions is a notion. This shows that the loop must continue to execute until it breaks. The break statements are then added inside the code block. Write true inside the conditional braces rather than declaring any Python variable, setting conditions, and then incrementing it.
weeklySalary = 0 |
Output:
Holiday!! |
While Loop with Else
The else statement can be used with loops in Python. When the while loop is used, the else sentence is only used if the condition changes to false.
a = 1 |
The example illustrates how the else statement works with the while loop.
Output: |
In the aforementioned example, the program continues to run the while loop's body until the condition is satisfied, which means that the value of an is less than 5. The condition is false when the program enters the loop for the fourth time when the value of an is increased from 4 to 5, as the initial value of an is 1 and its value is increased by 1 each time the program enters the loop. The program runs the condition as false after the fifth time it is checked, moves to the else block, and then executes the else block's body, indicating that the condition is now false.
Conditional Interruptions
The following two keywords are available in Python and can be used to end a loop iteration early.
- Break: The break keyword breaks the loop and moves control to its conclusion.
|
Output: |
2. Continue: The continue keyword ends the current iteration, moves control to the top of the loop, and performs a new evaluation of the loop condition. The following repetition begins if the condition is satisfied.
Example:
a = 1 |
Factorial with While Loop
num = int(input("Enter a number: ")) |