Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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

0
akohan
Asked:
akohan
  • 3
  • 2
2 Solutions
 
TrigunaCommented:
It is ok to use return. But best programming practice is to use break and then use return.
0
 
sunnycoderCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now