0% found this document useful (0 votes)
1 views3 pages

Module 3 Notes

A while loop repeatedly executes a block of code as long as a specified condition is true, with careful attention needed to avoid infinite loops. The loop's body runs until the condition becomes false, and the break statement can be used to exit the loop prematurely. Key points include proper initialization, condition updates, and the importance of indentation in Python.

Uploaded by

Nighat Iqbal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Module 3 Notes

A while loop repeatedly executes a block of code as long as a specified condition is true, with careful attention needed to avoid infinite loops. The loop's body runs until the condition becomes false, and the break statement can be used to exit the loop prematurely. Key points include proper initialization, condition updates, and the importance of indentation in Python.

Uploaded by

Nighat Iqbal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What is a while loop?

 A while loop repeatedly executes a block of code as long as a given condition remains True.

 It's like saying "Keep doing this while this condition is true."

Example Code:

x=0

while x < 5:

print("Not there yet, x=" + str(x))

x=x+1

print("x=" + str(x))

Explanation:

1. Initialization: We start by setting a variable x to 0.

2. Condition: The while x < 5: part is the condition. As long as x is less than 5, the loop
continues.

3. Loop Body: The indented lines under the while statement form the loop body. This code runs
repeatedly.

o print("Not there yet, x=" + str(x)): This line prints the current value of x.

o x = x + 1: This line is crucial! It increases the value of x by 1 in each iteration. Without


this, the loop would run forever because the condition (x < 5) would always be True.

4. Loop Termination: Eventually, x becomes 5. The condition x < 5 is no longer True, so the loop
stops, and the program continues to the next line after the loop.

5. Final Output: print("x=" + str(x)) This line prints the final value of x (which is 5).

Key Points:

 Indentation is Key: In Python, the indentation tells the computer which lines of code belong
inside the loop.

 Beware of Infinite Loops: Make sure your loop's condition will eventually become False;
otherwise, you'll have an infinite loop that never ends.
 op? An infinite loop occurs when a loop's condition is always true, causing it to run
forever. This can crash your program or make it unresponsive.
 Why do infinite loops happen? They usually happen because of a logic error in the
loop's condition or a missing update to the variable that the condition checks.
 How to avoid infinite loops:
o Careful Initialization: Make sure your loop variables are initialized correctly
before the loop starts.
o Condition Update: Ensure the condition used in your loop (e.g., while x > 0)
eventually becomes false. The variable being checked (in this case, x) should be
modified within the loop's body.
o Using break: The break statement immediately exits a loop, even if the
condition is still true. This is useful for breaking out of loops based on specific
events or conditions within the loop.
 Example of using break to stop an infinite loop:

while True:
user_input = input("Enter 'quit' to exit: ")
if user_input == "quit":
break
print("You entered:", user_input)

Infinite Loops: A Summary

 Definition: An infinite loop is a loop that never ends on its own. It keeps running the same
code block repeatedly because the exit condition is never met.

 Problem: Infinite loops can cause your program to freeze and may require you to force-quit.

 Example:

 while True:

print("This will print forever!")

Breaking Free: The break Statement

 Purpose: The break statement provides a way to exit a loop prematurely, even if the loop's
condition is still true.

 How it Works: When Python encounters break inside a loop, it immediately stops the loop
and moves on to the code after the loop.

 Example:

 while True:

 user_input = input("Enter 'quit' to stop: ")

 if user_input == 'quit':

 break # Exit the loop if the user types 'quit'

 print("You entered:", user_input)

print("Loop has ended.")

Key Points to Remember

 Infinite loops are often caused by logical errors in the loop's condition.

 Always double-check your loop conditions to make sure they will eventually become false.
 The break statement is a valuable tool for controlling the flow of your loops and preventing
infinite loops.

You might also like