Python Tutorial in Hindi #11 - While Loop

While Loop in Python – Learn how to use loops effectively for automation and iteration!


In this video by Mohit Decodes, we cover:

✅ What is a While Loop in Python?

✅ Syntax and Flow of While Loop

✅ Real-life use cases of While Loop

✅ Using break to exit loops

✅ Using continue to skip iterations

✅ Hands-on examples with real-time output


🧩 Example 1: Basic While Loop
# Basic While Loop Example
i = 1
while i <= 5:
print("Mohit Decodes", i)
i += 1


Output:

Mohit Decodes 1
Mohit Decodes 2
Mohit Decodes 3
Mohit Decodes 4
Mohit Decodes 5

🧩 Example 2: Infinite While Loop (Use with Caution)
# Infinite Loop Example
while True:
print("This will run forever...")


(Press Ctrl + C to stop the loop)

🧩 Example 3: While Loop with Condition
# Printing 1 to 10 using while loop
num = 1
while num <= 10:
print(num)
num += 1


Output:

1
2
3
4
5
6
7
8
9
10

🧩 Example 4: Using break in While Loop
# Example of break statement
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
print("Loop terminated at i =", i)


Output:

1
2
3
4
Loop terminated at i = 5

🧩 Example 5: Using continue in While Loop
# Example of continue statement
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)


Output:

1
2
4
5


(Notice that 3 is skipped due to continue.)

🧩 Example 6: While Loop with Else
# while loop with else block
i = 1
while i <= 5:
print(i)
i += 1
else:
print("Loop Completed Successfully!")


Output:

1
2
3
4
5
Loop Completed Successfully!