+ 20
[SOLVED]Can continue and break be used in functions?
if not what's the alternative?
18 Réponses
+ 20
If there's a while or for loop in your function, you can use break and continue.
But often it will be simpler to just 'return'.
+ 23
To go out of iterative repetition use break, to go out of recursive repetition use return, the function wants to return a value use also return.
+ 9
HonFu yeah well the code is something like this
https://code.sololearn.com/cLLaWk0r0goI/?ref=app
+ 7
HonFu nvm I figured it out.
thx for your help
+ 6
To leave a function u can simply use return.
+ 3
Big boss, break and continue are used in loops and statements.
if you include any loop in your function, then you can use either break or continue, else use return.
+ 2
Break and Continue are used in loops because the process of loop is to repeat some code multiple time.
So sometime you need to break from loop or skip some iteration by using continue.
In function there isn't any thing like repitation , function are called when required and they run once . So there is no require of break and continue.
Hope this helps☺️☺️.
+ 1
Sorry, I was distracted for a while.
So how did you decide? Break yes or no, and why?
I don't see why we shouldn't for, while and break to our heart's content within functions, but if after breaking the loop, the function does nothing different anyway than recognizing: 'Hey, my mission is over, so I guess I return', well then maybe you can just return directly without going the break detour.
It probably comes down to questions like how efficient or readable is the one way or the other.
+ 1
To leave a function you can just use return.
+ 1
If there's a while or for loop in your function, you can use break and continue.
But often it will be simpler to just 'return'.
+ 1
0
where you have while loop or for loop we can use break and continue statement
0
Continue and break can be used inside a function if you have while or for loop in your function.
return and pass are two alternatives to get out of a function. Return passes a output value from function and pass simply does nothing.
0
If there is a loop inside your function then you can use break and continue. Otherwise you can return or print inside the function.
0
Maybe continue, but break isn't is simpler to use return instead ?
0
These can be used if inside the body of the function there are loops involved, and only if both 'break' and 'continue' are placed inside those loops, but in general it is not recommended, an alternative to that is to use flags , and that these are present in the stop condition of the loop, and that when the flag is activated it allows exiting the loop by the stop condition and not by force, as in the case of using 'break' ; In the case of the use of 'continue', something similar can be done using a flag through an 'if' and if it is activated, it disables all the code block that is below and that you do not want to be executed.
0
Yes it can,if other functions are used along.
0
They can be used in loop into tour function. I think that people uses return more.