Introduction
A while loop is used to execute a block of code repeatedly as long as a given condition is True.
Instead of writing the same code multiple times, we can use a while loop to make our program shorter and easier to understand.
What Is a While Loop in Python?
A while loop is a control flow statement that executes a block of code repeatedly as long as the specified condition is True.
When the condition becomes False, the loop stops executing.
Basic Syntax of While Loop
The basic syntax of a while loop in Python is:
while condition: statements Explanation:
-
while: Keyword used to create a while loop. -
condition: Checks whether the condition is True or False. -
statements: The block of code executed while the condition is True.
Example of While Loop
Let's print numbers from 1 to 5 using a while loop.
i = 1 while i <= 5: print(i) i += 1 Output:
1 2 3 4 5 Explanation:
- First, we initialize the variable
i = 1. - The while loop checks whether
i <= 5. - If the condition is True, it prints the value of
i. - The statement
i += 1increases the value by 1. - When
ibecomes 6, the condition becomes False, and the loop stops.
Example 2: Print Even Numbers Using While Loop
We can also use a while loop to print even numbers.
i = 2 while i <= 10: print(i) i += 2 Output:
2 4 6 8 10 Here, the value of i increases by 2 in every iteration, so only even numbers are printed.
Example 3: Sum of Numbers Using While Loop
Let's calculate the sum of numbers from 1 to 5 using a while loop.
i = 1 total = 0 while i <= 5: total = total + i i += 1 print("Sum:", total) Output:
Sum: 15 Here, the loop adds each number to the total variable until the condition becomes False.
Infinite While Loop
An infinite while loop is a loop that continues executing forever because its condition never becomes False.
Example:
while True: print("Hello Python") This loop prints the message continuously because the condition is always True.
We can stop an infinite loop using the break statement.
Loop Control Statements in While Loop
Python provides three important statements that can change the normal execution of a while loop.
1. Break Statement
The break statement is used to stop the loop immediately.
Example:
i = 1 while i <= 5: if i == 3: break print(i) i += 1 Output:
1 2 Here, when i becomes 3, the break statement stops the loop.
2. Continue Statement
The continue statement skips the current iteration and moves to the next iteration.
Example:
i = 0 while i < 5: i += 1 if i == 3: continue print(i) Output:
1 2 4 5 Here, when i becomes 3, that iteration is skipped.
3. Pass Statement
The pass statement does nothing. It is used as a placeholder when we want to write a loop without adding any statements yet.
Example:
i = 1 while i <= 5: if i == 3: pass print(i) i += 1 Output:
1 2 3 4 5 Here, the pass statement does not affect the execution of the loop.
Why Is While Loop Important in Python?
While loops are useful because:
- They reduce code repetition.
- They execute code until a condition becomes False.
- They are useful when the number of iterations is not known in advance.
- They help automate repetitive tasks.
- They make programs easier to manage.
