For Loop With Break not working as expectations

I use “For Loop With Break” iterate from 0 to 10, and break when index == 2; blueprint details is shown below:

it will print [0, 1, 2] rather than [0, 1]. The “Sequence Then 1” will still work when I Break it in “Then 0” !!!

By viewing the macro implementation of “For Loop with Break”, I know why it works as before. But in my mind, it is not a standard loop-break. for example in C++, “cout << i” will not execute after break statement.

for (int i = 0; i < 10; i++)
{
if (i == 2) break;
cout << i;
}

so my question is that: why design the “For Loop with Break” like that? Is there a more common loop-break node which works like C++.

I believe :crossed_fingers: you’re blaming the wrong node. It’s the Sequence that obfuscates the execution flow here. If you want to use Else, use False on the Branch instead of the Sequence. That should be the blueprint equivalent of your code which simply does not have a Sequence analogue.


You can always add your own macros / functions, of course.

But yes, Epic took some liberties and BPs may seem backwards at times, especially for someone well versed in more traditional languages. The utter lack of short-circuit evaluation is yet another feature to trip up on…

1 Like

To add more information to what @Everynone said, the Sequence node is not affected by outside flow control. Once called, all execution pins in a sequence node are prepared to be fired.

The for loop is indeed breaking when you call “break”, but what’s confusing is that the Sequence node has already stacked all subsequent pins to be executed regardless of the loop or anything else. It’s not the same as having code in C++ after the “break;”.

So yes, if you want control inside a For Loop With Break or anything else similar to it, do as Everynone said and use the “False” on the control branch.

1 Like

The Sequence is the problem. Breaking the loop not affect the next iteration of sequence. Your code should look like this