[SOLVED]Can continue and break be used in functions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 20

[SOLVED]Can continue and break be used in functions?

if not what's the alternative?

24th Jul 2018, 8:21 PM
LONGTIE👔
LONGTIE👔 - avatar
18 Answers
+ 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'.
24th Jul 2018, 8:34 PM
HonFu
HonFu - avatar
+ 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.
25th Jul 2018, 12:58 PM
Prop de Puppy
Prop de Puppy - avatar
+ 9
HonFu yeah well the code is something like this https://code.sololearn.com/cLLaWk0r0goI/?ref=app
24th Jul 2018, 8:43 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 7
HonFu nvm I figured it out. thx for your help
24th Jul 2018, 9:02 PM
LONGTIE👔
LONGTIE👔 - avatar
+ 6
To leave a function u can simply use return.
25th Jul 2018, 12:44 PM
Oma Falk
Oma Falk - avatar
+ 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.
26th Jul 2018, 8:50 AM
Enock Acquah
Enock Acquah - avatar
+ 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☺️☺️.
25th Jul 2018, 10:18 AM
Meet Mehta
Meet Mehta - avatar
+ 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.
24th Jul 2018, 9:32 PM
HonFu
HonFu - avatar
+ 1
To leave a function you can just use return.
26th Jul 2018, 2:56 AM
Vignesh
+ 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'.
20th Jan 2022, 12:52 PM
Ruchit Prajapati
Ruchit Prajapati - avatar
+ 1
⁣ ⁣
23rd Apr 2022, 6:15 PM
Baran Kul
Baran Kul - avatar
0
where you have while loop or for loop we can use break and continue statement
25th Jul 2018, 1:40 PM
Dare$devil
Dare$devil - avatar
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.
28th Mar 2021, 6:42 AM
Prashant Mane
Prashant Mane - avatar
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.
29th Oct 2021, 7:14 AM
Bikesh Shrestha
0
Maybe continue, but break isn't is simpler to use return instead ?
11th Apr 2022, 6:07 AM
[No Name]
[No Name] - avatar
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.
17th Jul 2022, 6:53 PM
Cristian Morales
Cristian Morales - avatar
0
Yes it can,if other functions are used along.
29th Aug 2022, 11:30 AM
Prospera Idyorough
Prospera Idyorough - avatar
0
They can be used in loop into tour function. I think that people uses return more.
28th Nov 2022, 10:42 AM
Jaime
Jaime - avatar