Use goto
:
This will only exit the while
loop.
while (true) {
...
if (some condition) {
goto CLEANUP;
}
...
}
CLEANUP:
...
exit()
the program:
while (true) {
...
if (some condition) {
exit (EXIT_FAILURE);
/* OR, if in the `main()` function: */
return EXIT_FAILURE;
}
....
}
abort()
the program:
while (true) {
...
if (some condition) {
abort ();
}
...
}
Some other alternatives, that will do much more than just exit the while
loop, include:
- The
raise()
function. - The
quick_exit()
function. - The
_Exit()
function. - The
_exit
function. (Note that this is not defined in the C standard, it is a POSIX extension)
0
solved while loop exit, without well-known way [closed]