I\'m currently trying to figure out a way to break out of a for
loop from within a function called in that loop. I\'m aware of the possibility to just have the
Just set a global variable and check that on the loop:
#include <stdio.h>
int leave = 0;
void foo (int a) {
printf("a: %d", a);
leave = 1;
}
int main(void) {
for (int i = 0; i <= 100; i++) {
foo(i);
if (leave)
break;
}
return 0;
}
This question has already been answered, but I think it is worth delving into all of the possible options to exit a loop in c++. There are basically five possibilities:
break
conditionreturn
conditiongoto
In the following, I will describe use cases for these options using c++14. However, you can do all of these in earlier versions of c++ (except maybe exceptions). To keep it short, I will omit the includes and the main function. Please comment, if you think some part needs more clarity.
The standard way to exit a loop is a loop condition. The loop condition is written in the middle part of a for
statement, or between the parentheses of a while
statement:
for(something; LOOP CONDITION; something) {
...
}
while (LOOP CONDITION)
...
}
do {
...
} while (LOOP CONDITION);
The loop condition decides if the loop should be entered, and if the loop should be repeated. In all of the above cases, the condition has to be true
, for the loop to be repeated.
As an example, if we want to output the number from 0 to 2, we could write the code using a loop and a loop condition:
for (auto i = 0; i <= 2; ++i)
std::cout << i << '\n';
std::cout << "done";
Here the condition is i <= 2
. As long as this condition evaluates to true
, the loop keeps running.
An alternative implementation would be to put the condition into a variable instead:
auto condition = false;
for (auto i = 0; !condition; ++i) {
std::cout << i << '\n';
condition = i > 2;
}
std::cout << "done";
Checking the output for both versions, we get the desired result:
0
1
2
done
How would you use a loop condition in an real world application?
Both versions are widely used inside c++ projects. It is important to note, that the first version is more compact and therefore easier to understand. But the second version is usually used if the condition is more complex or needs several steps to be evaluated.
For example:
auto condition = false;
for (auto i = 0; !condition; ++i)
if (is_prime(i))
if (is_large_enough(i)) {
key = calculate_cryptographic_key(i, data);
if (is_good_cryptographic_key(key))
condition = true;
}
break
conditionAnother simple way to exit a loop, is to use the break
keyword. If it is used inside the loop, the execution will stop, and continue after the loop body:
for (auto i = 0; true; ++i) {
if (i == 3)
break;
std::cout << i << '\n';
}
std::cout << "done";
This will output the current number, and increment it by one, until i
reaches a value of 3. Here the if
statement is our break
condition. If the condition is true
, the loop is broken (note the !
) and the execution continues with the next line, printing done
.
Doing the test, we indeed get the expected result:
0
1
2
done
It is important, that this will only stop the innermost loop in the code. Therefore, if you use multiple loops, it can lead to undesired behaviour:
for (auto j = 0; true; ++j)
for (auto i = 0; true; ++i) {
if (i == 3)
break;
std::cout << i << '\n';
}
std::cout << "done";
With this code we wanted to get the same result as in the example above, but instead we get an infinite loop, because the break
only stops the loop over i
, and not the one over j
!
Doing the test:
0
1
2
0
1
2
...
How would you use a break
condition in an real world application?
Usually break
is only used to skip parts of an inner loop, or to add an additional loop exit.
For example, in a function testing for prime numbers, you would use it to skip the rest of the execution, as soon as you found a case where the current number is not prime:
auto is_prime = true;
for (auto i = 0; i < p; ++i) {
if (p%i == 0) { //p is dividable by i!
is_prime = false;
break; //we already know that p is not prime, therefore we do not need to test more cases!
}
Or, if you are searching a vector of strings, you usually put the maximum size of the data in the loop head, and use an additional condition to exit the loop if you actually found the data you are searching for.
auto j = size_t(0);
for (auto i = size_t(0); i < data.size(); ++i)
if (data[i] == "Hello") { //we found "Hello"!
j = i;
break; //we already found the string, no need to search any further!
}
return
conditionThe return
keyword exits the current scope and returns to the calling function. Thus it can be used to exit loops, and in addition, give back a number to the caller. A common case is to use return
to exit a loop (and its function) and return a result.
For example, we can rewrite the is_prime
function from above:
auto inline is_prime(int p) {
for (auto i = 0; i < p; ++i)
if (p%i == 0) //p is dividable by i!
return false; //we already know that p is not prime, and can skip the rest of the cases and return the result
return true; //we didn't find any divisor before, thus p must be prime!
}
The return
keyword can also be used to exit multiple loops:
auto inline data_has_match(std::vector<std::string> a, std::vector<std::string> b) {
for (auto i = size_t(0); i < a.size(); ++i)
for (auto j = size_t(0); j < a.size(); ++j)
if (a[i] == b[j])
return true; //we found a match! nothing to do here
return false; //no match was found
}
How would you use a return
condition in an real world application?
Inside smaller functions, return
is often used to exit loops and directly return results. Furthermore, inside larger functions, return
helps to keep the code clear and readable:
for (auto i = 0; i < data.size(); ++i) {
//do some calculations on the data using only i and put them inside result
if (is_match(result,test))
return result;
for (auto j = 0; j < i; ++j) {
//do some calculations on the data using i and j and put them inside result
if (is_match(result,test))
return result;
}
}
return 0; //we need to return something in the case that no match was found
Which is much easier to understand, than:
auto break_i_loop = false;
auto return_value = 0;
for (auto i = 0; !break_i_loop; ++i) {
//do some calculations on the data using only i and put them inside result
if (is_match(result,test)) { //a match was found, save the result and break the loop!
return_value = result;
break;
}
for (auto j = 0; j < i; ++j) {
//do some calculations on the data using i and j and put them inside result
if (is_match(result,test)) { //a match was found, save the result, break the loop, and make sure that we break the outer loop too!
return_value = result;
break_i_loop = true;
break;
}
}
if (!break_i_loop) //if we didn't find a match, but reached the end of the data, we need to break the outer loop
break_i_loop = i >= data.size();
}
return return_value; //return the result
Exceptions are a way to mark exceptional events in your code. For example, if you want to read data from a file, but for some reason the file doesn't exist! Exceptions can be used to exit loops, however the compiler usually generates a lot of boilerplate code to safely continue the program if the exception is handled. Therefore exceptions shouldn't be used to return values, because it is very inefficient.
How would you use an exception in an real world application?
Exceptions are used to handle truly exceptional cases. For example, if we want to calculate the inverse of our data, it might happen that we try to divide by zero. However this is not helpful in our calculation, therefore we write:
auto inline inverse_data(std::vector<int>& data) {
for (auto i = size_t(0); i < data.size(); ++i)
if (data[i] == 0)
throw std::string("Division by zero on element ") + std::to_string(i) + "!";
else
data[i] = 1 / data[i];
}
We can the handle this exception inside the calling function:
while (true)
try {
auto data = get_user_input();
inverse = inverse_data(data);
break;
}
catch (...) {
std::cout << "Please do not put zeros into the data!";
}
If data
contains zero, then inverse_data
will throw an exception, the break
is never executed, and the user has to input data again.
There are even more advance options for this kind of error handling, with additional error types, ..., but this is a topic for another day.
** What you should never do! **
As mentioned before, exceptions can produce a significant runtime overhead. Therefore, they should only be used in truly exceptional cases. Although it is possible to write the following function, please don't!
auto inline next_prime(int start) {
auto p = start;
try {
for (auto i = start; true; ++i)
if (is_prime(i)) {
p = i;
throw;
}
}
catch (...) {}
return p;
}
goto
The goto
keyword is hated by most programmers, because it makes code harder to read, and it can have unintended side effects. However, it can be used to exit (multiple) loops:
for (auto j = 0; true; ++j)
for (auto i = 0; true; ++i) {
if (i == 3)
goto endloop;
std::cout << i << '\n';
}
endloop:
std::cout << "done";
This loop will end (not like the loop in part 2), and output:
0
1
2
done
How would you use a goto
in an real world application?
In 99.9% of the cases there is no need to use the goto
keyword. The only exceptions are embedded systems, like an Arduino, or very high performance code. If you are working with one of these two, you might want to use goto
to produce faster or more efficient code. However, for the everyday programmer, the downsides are much bigger than the gains of using goto
.
Even if you think your case is one of the 0.1%, you need to check if the goto
actually improves your execution. More often than not, using a break
or return
condition is faster, because the compiler has a harder time understanding code containing goto
.
break
, like goto
, can only jump locally within the same function, but if you absolutely have to, you can use setjmp
and longjmp
:
#include <stdio.h>
#include <setjmp.h>
jmp_buf jump_target;
void foo(void)
{
printf("Inside foo!\n");
longjmp(jump_target, 1);
printf("Still inside foo!\n");
}
int main(void) {
if (setjmp(jump_target) == 0)
foo();
else
printf("Jumped out!\n");
return 0;
}
The call to longjmp
will cause a jump back to the setjmp
call. The return value from setjmp
shows if it is returning after setting the jump target, or if it is returning from a jump.
Output:
Inside foo!
Jumped out!
Nonlocal jumps are safe when used correctly, but there are a number of things to think carefully about:
longjmp
jumps "through" all the function activations between the setjmp
call and the longjmp
call, if any of those functions expect to be able to do additional work after the current place in execution, that work will simply not be done.setjmp
has terminated, the behaviour is undefined. Anything can happen.setjmp
hasn't yet been called, then jump_target
is not set, and the behaviour is undefined.setjmp
can under certain conditions have undefined values.setjmp
call.Most of these follow naturally if you have a good understanding of what a nonlocal jump does on the level of machine instructions and CPU registers, but unless you have that, and have read what the C standard does and does not guarantee, I would advise some caution.
(Note: the question has been edited since I originally wrote this)
Because of the way C is compiled it must know where to break to when the function is called. Since you can call it from anywhere, or even somewhere a break makes no sense, you cannot have a break;
statement in your function and have it work like this.
Other answers have suggested terrible solutions such as setting a global variable, using a #define
or longjumping(!) out of the function. These are extremely poor solutions. Instead, you should use the solution you wrongly dismiss in your opening paragraph and return a value from your function that indicates the state that you want to trigger a break
in this case and do something like this:
#include <stdbool.h>
bool checkAndDisplay(int n)
{
printf("%d\n", n);
return (n == 14);
}
int main(void) {
for (int i = 0; i <= 100; i++) {
if (checkAndDisplay(i))
break;
}
return 0;
}
Trying to find obscure ways to achieve things like this instead of using the correct way to achieve the same end result is a surefire way to generate dire quality code that is a nightmare to maintain and debug.
You mention, hidden in a comment, that you must use a void return, this is not a problem, just pass the break parameter in as a pointer:
#include <stdbool.h>
void checkAndDisplay(int n, bool* wantBreak)
{
printf("%d\n", n);
if (n == 14)
wantBreak = true;
}
int main(void) {
bool wantBreak = false;
for (int i = 0; i <= 100; i++) {
checkAndDisplay(i, &wantBreak);
if (wantBreak)
break;
}
return 0;
}
Since your parameters are fixed type I suggest you use a cast to pass in the pointer to one of the parameters, e.g. foo(a, b, (long)&out);
break
is statement which is resolved during compile time. Therefore the compiler must find appropriate for/while loop within the same function. Note that there is no guarantee that the function couldn't be called from somewhere else.
Consider inlining your function manually in the for
loop. If this function is called in multiple loops, define it as a macro:
#define f()\
printf("a: %d", a);\
break;