Matteo Ferla
Matteo Ferla

Reputation: 2378

Is there any language that allows a break through multiple loops?

break interrupts a for-loop in most languages, but in the case of nested loops I have never encountered an n-th order break.
1. Is there such a thing in any language?
2. If so what is the correct name for it?
3. If not, why?
NB. I am not looking for workarounds.

Regarding point 3. The closest thing I know is goto, which should not be used as it leads to spaghetti code (Python has it only in a joke module), but this seems like a different problem as a boolean variable to mark an inner break, catching a raised a custom error or moving the block to a function in order to break with return are a lot more convoluted (in terms of line numbers and variables in the code).

(This is a curiosity question from a theoretical point of view, but if it helps, I code primarily in Python, Matlab and JS. I have know Perl, Pascal and Basic, but I know only the basics of C++ and know shamefully little of machine code.)

Upvotes: 3

Views: 288

Answers (6)

PinkTurtle
PinkTurtle

Reputation: 7041

I don't know of any language that lets you do this (apart from @dasblinkenlight example - and not saying there aren't any) but you can easily emulate it in any language that has the break statement.

I.e. conditionally break on a boolean exit loop var.

var exitLoops = false;
for (var i = 0; i < x; i++) {
    for (var j = 0; j < y; j++) {
        for (var k = 0; k < z; k++) {
            if (something) {
                exitLoops = true;
                break;
            }
        }
        if (exitLoops) break;
    }
    if (exitLoops) break;
}

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263557

There are several examples that haven't been mentioned.

Perl has a labeled break -- though Perl spells it last. The target is the name of a label at the top of the loop. For example:

#!/usr/bin/perl

use strict;
use warnings;

OUTER:
foreach my $i (1..3) {
    foreach my $j (1..3) {
        print "(i=$i j=$j) ";
        if ($i == 2 and $j == 2) {
            last OUTER;
        }
    }
    print("\n");
}
print "\n";

Ada has a similar construct, but the target is the name of the loop, which is distinct from a label that can be the target of a goto statement (and Ada spells break as exit):

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Exit_Outer is
begin
    Outer:
    for I in 1 .. 3 loop
        for J in 1 .. 3 loop
            Put("(I="); Put(I, Width => 0);
            Put(" J="); Put(J, Width => 0);
            Put(") ");
            if I = 2 and J = 2 then
                exit Outer;
            end if;
        end loop;
        New_Line;
    end loop Outer;
end Exit_Outer;

Upvotes: 0

Rand00
Rand00

Reputation: 376

PHP allows for "multi-level breaks" (eg. break 3 which breaks you out of three levels of loops), these are discussed here:

https://www.php.net/manual/en/control-structures.break.php

Bash also has this functionality:

https://tldp.org/LDP/abs/html/loopcontrol.html

(The term "multi-level break" appears in a 2008 article in Mathematics of Program Construction, but I don't believe this is the first appearance of the terminology: https://dl.acm.org/doi/abs/10.1007/978-3-540-70594-9_11)

Upvotes: 1

Suman Barick
Suman Barick

Reputation: 3411

In JavaScript you can do this

Copy paste the following the Chrome Dev Console,

free: 
for(var i=0; i<10; i++) {
  for(var j=0; j<10; j++) {
    for(var k=0;k<10;k++){
      console.log('I am at i='+i+' , j='+j+ ' , k='+k);
      if(k==3) { 
        console.log('I will now break FREE...');
        break free;
      }
    }
  }
}

console.log('... And Now I am Here...')

Output

I am at i=0 , j=0 , k=0
I am at i=0 , j=0 , k=1
I am at i=0 , j=0 , k=2
I am at i=0 , j=0 , k=3
I will now break FREE...
... And Now I am Here...

Upvotes: 0

DrWael
DrWael

Reputation: 247

No there isn't (as far as i know). And why ? because if you need to exit several nested for loops all at once, then you have a code design problem, not a syntax problem. all the answers given above, except of @PinkTurtle , uses some sort of goto statement , which is not recommended .

Upvotes: -1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726929

Java has a labeled break statement that lets you break out of any number of loops:

search:
    for (i = 0; i < arrayOfInts.length; i++) {
        for (j = 0; j < arrayOfInts[i].length;
             j++) {
            if (arrayOfInts[i][j] == searchfor) {
                foundIt = true;
                break search; // <<=== This statement breaks both nested loops
            }
        }
    }

Upvotes: 6

Related Questions