• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Discussion [Suggestion] Nested break

gnysek

Member
Currently, in GM you need to write:
Code:
var _found = false;
for(var i=0; i<10; i++) {
    for (var j=0; j<10; j++) {
        if (a[i, j] == 5) {
         _found = true;
         break;
      }
    }
    if (_found) break;
}
Some other languages, allow to put number to exit a number of for-loops:
Code:
for(var i=0; i<10; i++) {
    for (var j=0; j<10; j++) {
        if (a[i, j] == 5) break 2;
    }
}
It would be a nice addition to GML syntax.
Code:
break N; // goes out from N loops, skipping further actions
 

GMWolf

aka fel666
Better than break N would be the label syntax like in java.
Code:
outer: for(...) {
  for(...) {
    break outer;
   }
}
 

xot

GMLscripter
GMC Elder
That's not bad. JavaScript and Go work that way too. I definitely prefer labels over some literal depth value.
 
A

Annoyed Grunt

Guest
Some people say you should never use break or continue. I disagree and use them fairly often, but I think that if you are at the point where you need such noisy features as multiple-level breaks then you should focus more on refactoring your code. You can make a very small change to the OP example to make it not require breaks at all, for example:

Code:
var _found = false;
for(var i = 0; i < 10 and !_found; i++) {
    for(var j = 0; j < 10 and !_found; j++) {
        _found = (a[i, j] == 5);
    }
}
 
  • Like
Reactions: xot
Top