0
Can someone exlpain loop?
while loops and for loops.
3 Answers
+ 2
Loops are used to run a certain block of code continuously until the condition satisfied. There are two types of loops in python
1. while loop
2. for loop
they both do pretty same thing only the usage and syntax differ
1. while loop
While loop loops a block of code within it until the passed on condition becomes false
example
count = 0
while count < 5:
print(count)
count=count + 1
this loop first print '0' and check for the condition
0 < 5 which is true so it enters the block of code and increment itself and come back to loop again only at 5 the loop becomes false and gets exits
till that it keep on running.
2. for loop
for loop does the same but we can use pretty cool things like range functions in for loop, this comes in handy to loop anything u want
example
for i in 'hello':
print(i)
here the i is temperory variable to access within for loop
it prints
h
e
l
l
o
0
You need to be specific. What part of while or for loop you don't understand?
0
@sundar. i really appreciate it. I am a beginner so THANKS for the help.