
#pythonloops
Loops in Python
A for loop is useful when we want to repeat something a fixed number of times or go through items in a sequence.
for i in range(5):
print(i)Output
0
1
2
3
4While Loop
A while loop runs as long as its condition is true.
i = 1
while i <= 5:
print(i)
i += 1Output
1
2
3
4
5Break and Continue
Sometimes we need to control how a loop works.
Break
break is used to stop the loop completely.
for i in range(5):
if i == 3:
break
print(i)Continue
continue skips the current iteration and moves to the next one.
for i in range(5):
if i == 2:
continue
print(i)Join Techsnap Creators
Share your knowledge and earn ??
Want to showcase your tech expertise and get rewarded for your insights? Join the Techsnap creator network!
Write insightful blogs, stay ahead of industry trends, and grow your professional brand while helping others in the community.
Ready to make an impact?

Comments