Alex Rivera | Logout

How to break out of a function

Asked 2011-06-10T05:12:35.430
28

If I have a function as follows:

void func () {
    //...

    if (condition) {
        break;
    }
}

When I use break it gives me an error. Is there another way to exit a function using an if condition and to complete compiling the code normally?

Edit
Report

3 Answers

12

Try to use 'return' in place of break when you want to run rest of code normally.

Use 'break' in case of switch or for loop for normal execution

Use 'exit' for force stop in execution

answered 2011-06-10T05:17:28.683
4

Just use return.

More info can be found here.

answered 2011-06-10T05:15:13.260
2

In C++, you can return from a function any time you want.

answered 2011-06-10T05:13:41.587

Your Answer