• C

exiting a loop

Hell group,

I need to break a loop in a called function. Is that ok to use return in loop?! since I don't want to create an extra if block after the while loop and call return.

Any idea?

Regards,

main()
{
   func1();
}
 
int func1() {
 
  while loop {
    //Is it ok to use return here rather than using break?\
    return -1;
  }
}

Open in new window

akohanAsked:
Who is Participating?
 
TrigunaConnect With a Mentor Commented:
It is ok to use return. But best programming practice is to use break and then use return.
0
 
sunnycoderConnect With a Mentor Commented:
You can return from within a loop but that means that entire function would return ... If you want to execute something after loop exit and before function return the use break to break out of the loop. Using a break would transfer the control to code that immediately follows the end of loop.
0
 
TrigunaCommented:
more explanantion hum...
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
sunnycoderCommented:
If you factor in the more explanation part, it would explain the small difference in posting times too.
0
 
TrigunaCommented:
agreed.
0
 
akohanAuthor Commented:

Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.