The while loop is a control structure used in programming to repeat a block of code as long as a specified condition remains true. It is particularly useful when you don’t know beforehand how many times you need to repeat a block of code.
Syntax
while condition:    # Code to execute repeatedly- condition: This is an expression that is evaluated before each iteration of the loop. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop stops.
 
How It Works
- Condition Evaluation: Before each iteration, the condition is checked.
 - Code Execution: If the condition is true, the code inside the loop is executed.
 - Repeat: After executing the code block, the condition is evaluated again. If it is still true, the loop continues; otherwise, the loop terminates.
 
Example 1: Basic While Loop
count = 0while count < 5:    print("Count is:", count)    count += 1Output:
Count is: 0Count is: 1Count is: 2Count is: 3Count is: 4Explanation:
- The loop starts with 
count = 0. - The condition 
count < 5is true, so the loop prints the current value ofcountand then increments it by 1. - This process repeats until 
countreaches 5, at which point the conditioncount < 5becomes false, and the loop terminates. 
Example 2: Infinite Loop
If the condition never becomes false, the loop will run indefinitely. This is called an infinite loop.
while True:    print("This will run forever!")Explanation:
- The condition 
Trueis always true, so this loop never ends. You typically usebreakto exit such loops manually. 
Example 3: Using break to Exit a Loop
The break statement can be used to exit a loop prematurely, even if the condition is still true.
count = 0while count < 10:    print("Count is:", count)    if count == 5:        break    count += 1Output:
Count is: 0Count is: 1Count is: 2Count is: 3Count is: 4Count is: 5Explanation:
- The loop is designed to run until 
countreaches 10. - However, when 
countequals 5, thebreakstatement is executed, causing the loop to terminate early. 
Example 4: Using continue to Skip an Iteration
The continue statement skips the rest of the code inside the loop for the current iteration and moves on to the next iteration.
count = 0while count < 5:    count += 1    if count == 3:        continue    print("Count is:", count)Output:
Count is: 1Count is: 2Count is: 4Count is: 5Explanation:
- When 
countequals 3, thecontinuestatement is executed, causing the loop to skip theprintstatement for that iteration. 
Common Pitfalls
- Infinite Loops: Make sure the condition will eventually become false, or include a 
breakstatement to prevent the loop from running forever. 
Key Points
- A 
whileloop is used when you want to repeat an action as long as a condition is true. - Ensure that the loop condition will eventually become false to avoid infinite loops.
 - Use 
breakto exit a loop prematurely andcontinueto skip an iteration. 
Understanding the while loop is essential for writing programs that need to repeat actions based on dynamic conditions, making it a fundamental concept in programming.



0 Comments
Please do note create link post in comment section