+ 4
what is different between break and continue)
4 odpowiedzi
+ 6
Break is used to exit the loop while continue is used to skip to the next iteration.
+ 6
The tutorials here explain this.
+ 2
number = 0
for number in range(10):
if number == 5:
break # break here
print('Number is ' + str(number))
--- output ---
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
------------------------------------------------------------------------
number = 0
for number in range(10):
if number == 5:
continue # continue here
print('Number is ' + str(number))
--- output ---
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 6
Number is 7
Number is 8
Number is 9
+ 1
Break is used to get out of the loop .... continue doesn't skip elements understand this .....
If you write print () below continue then it. Will work ....but if you put print() above continue then it will not skip element.
Basically continue take the pointer return to the first line of loop .....so whatever rests below continue statement it will not executed because pointer goes to first line of the loop after continue ....so automatically it will be skipped .
I hope this will help you to understand difference.