Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break out of a function

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?

like image 760
Shadi Avatar asked Jun 10 '11 05:06

Shadi


3 Answers

break is used in loops and switch statement. use return instead.

like image 71
Headshota Avatar answered Nov 01 '22 07:11

Headshota


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

like image 26
Stuti Avatar answered Nov 01 '22 07:11

Stuti


use return;:

if(/*condition*/) { return; }

like image 40
AC2MO Avatar answered Nov 01 '22 08:11

AC2MO