+ 3

Difference between break and return.

I'm confuse about both of them it seems like both are similar ain't they?

23rd Oct 2018, 1:17 PM
Prince
Prince - avatar
6 Respuestas
+ 6
@Paul Jacobs good explanation. @Prince just don't confund break and continue either. break just go outside loop and continue skip the iteration but still keeps in loop. I recommend with you have any doubts when try to learning read python3 documentation. And don't rush it. If you still don't understand post in Q&A section again. I will like to try answer. Here is a link for more reference about control flow tools. https://docs.python.org/3/tutorial/controlflow.html (Python3 documentation)
23rd Oct 2018, 1:54 PM
Anya
Anya - avatar
+ 4
Return is used in functions. Break in loops. Return has a value. Break doesn't. At least that is the way I use them.
23rd Oct 2018, 1:24 PM
Paul
Paul - avatar
+ 3
code after return wont run inside a function for example: def func(): x = 123 return x print(x) when you call func() it wont print anything since print() is after return break is used to ”break” loops and return is used to return a value from a function
23rd Oct 2018, 2:23 PM
Juho Pesonen
Juho Pesonen - avatar
+ 1
No. They are completely different things. return (which is used only inside functions) can return a value from a function and store in a variable if you have provided 1. But break is used on loops. an instance return is used. #initializing a function def makeItThree(): return 3 #initizing variables n2 = makeItThree() n3 = makeItThree print(n2) #3 print(n3) #3 #the reason is because the the variables have stored the value returned by the function - which is 3 here. And the break thing for i in range(1,10): print(i) if i == 5: break #1 #2 #3 #4 #5 According to for loop it should print numbers from 1 - 10 . But break has stopped it after i is 5. SIDE NOTE: using a return statement in a loop could also stop the loop from running. So use it wisely
24th Oct 2018, 8:11 AM
Seniru
Seniru - avatar
0
break is used to stop a loop (while, for) return is used to return a value of function or methods of class
25th Oct 2018, 2:34 AM
danvetio
danvetio - avatar
0
For Break, please find the link below https://youtu.be/yCZBnjF4_tU This should be useful. And Return, the return statement allows you to terminate the execution of a function before you reach the end.
5th Nov 2018, 5:41 PM
Nikhil S kumar
Nikhil S kumar - avatar