+ 3
What's the difference between break and continue statements?
Hey, I was writing a program in python 3 and it wasn't working until I realized that the break statements were forcing the program to leave the for loop. So I think I'm a little confused between what continue statements versus break statements do. Can anyone help clarify?
11 Answers
+ 3
Short answer: Break means that the loop ends. Continue means to stop and go back to the begging of the loop executing the next iteration.
Long answer with example:
n = 1
x = 0
while True:
if n > x:
break
else:
print('hello')
This example the loop stops completely and never executes 'hello' because n(1) is greater than x(0)
n = 1
x = 0
while True:
if n > x:
continue
else:
print('hello')
In this example the program does not executes 'hello' because n(1) is greater than x(0) HOWEVER the loop does not stop it just go back to the beginning and continues the loop. In this example the loop never stops it is infinite. Not useful program just did it to illustrate what break and continues does.
+ 2
You can reach out to me here it's fine. What's up?
0
Awesome thanks for clearing that up
0
Hey ava, is there any to contact you, i am having problems in phyton,
0
Ok. Can you tell me what is the problem here, i=1
While i<=1:
Print (i)
I=i+1
Print ("Finished")
0
It says syntax error
0
And tell me how do I run the program, after writing print. What to do
0
lol
0
You're while and print statements need to be lowercase in order for the code to work
0
Python is a case sensitive language