0% found this document useful (0 votes)
5 views4 pages

Understanding Python While Loops

A while loop in Python is a control structure that executes a block of code repeatedly as long as a specified condition is true. It consists of four main parts: initialization, condition, body, and update, with the potential for infinite loops if the update is neglected. The document also explains the use of 'break' and 'continue' statements within while loops with examples.

Uploaded by

Riaz mir20
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)
5 views4 pages

Understanding Python While Loops

A while loop in Python is a control structure that executes a block of code repeatedly as long as a specified condition is true. It consists of four main parts: initialization, condition, body, and update, with the potential for infinite loops if the update is neglected. The document also explains the use of 'break' and 'continue' statements within while loops with examples.

Uploaded by

Riaz mir20
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

While Loop in Python

🔹 1. What is While Loop?


👉 A while loop is a control structure that repeats a block of code as long as a condition is
True.
It checks the condition before each repetition.

Syntax:

while condition:
# statements

✅ If the condition is True, code runs.


❌ If the condition is False, loop stops.

Example:
count = 1
while count <= 5:
print("Hello", count)
count = count + 1

Output:

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5

🔹 2. Parts of While Loop


A while loop has 4 main parts:

1. Initialization – set starting value.


Example: count = 1
2. Condition – checked before each iteration.
Example: count <= 5
3. Body – code to run repeatedly.
Example: print("Hello", count)
4. Update/Change – update loop variable, otherwise infinite loop.
Example: count = count + 1

🔹 3. Analysis of While Loop


Let’s analyze the above example step by step:

 Start → count = 1
 Check count <= 5 → True → Run body → Print Hello 1 → Update to 2
 Check count <= 5 → True → Print Hello 2 → Update to 3
 … continues until count = 6
 Now condition 6 <= 5 → False → Loop stops ✅

🔹 4. Each Part Explained with Example


🟢 Initialization
i = 1 # loop starts from 1

🟢 Condition
while i <= 5: # loop runs until i becomes greater than 5

🟢 Body
print(i) # code inside loop

🟢 Update
i = i + 1 # increase i each time

Full Example:
i = 1
while i <= 5:
print(i)
i = i + 1

Output:

1
2
3
4
5

🔹 5. Infinite Loop (⚠️)


If you forget to update the variable, loop never ends.

x = 1
while x <= 5:
print("Hi")

👉 This will print "Hi" forever because x never changes.

🔹 6. Break and Continue with While Loop


Example with break:
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1

Output:

1
2
3
4

Example with continue:


i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)

Output:

1
2
4
5

You might also like