Skip to main content
added 666 characters in body
Source Link
Roman
  • 49.8k
  • 2
  • 57
  • 131

Throw/Catch would probably work:

Catch[
  Do[
    Do[Print[{i, j}];
       If[j == 7, Throw[7]],
    {j, 10}],
  {i, 10}]]

(*    {1,1}    *)
(*    {1,2}    *)
(*    {1,3}    *)
(*    {1,4}    *)
(*    {1,5}    *)
(*    {1,6}    *)
(*    {1,7}    *)

(*    7    *)

The advantage of Throw over Break is that the latter quits only the innermost loop, whereas for the former you can decide how many loops to quit by setting the Catch appropriately.

Update: you seem to want to break only the outer loop but let the inner one finish the current iteration. This could work:

X = {{-10, 1, 1}, {8, 2, 2}, {-7, 3, 1}, {2, 4, 2}};
stop = False;
XX = Reap[Do[Do[If[X[[k, 1]] < 0, R = m*X[[k, 2]]; Sow[R];
                If[R == 9, stop = True], R = m*X[[k, 3]];
                Sow[R]]; If[R == 9, stop = True], {k, 1, 4}];
             If[stop, Break[]], {m, 1, 5}]][[2, 1]]
(*    {1, 2, 3, 2, 2, 4, 6, 4, 3, 6, 9, 6}    *)

The trick here is to set a flag stop in the inner loop, and at the end of the inner loop check this flag and conditionally break the outer loop.

Throw/Catch would probably work:

Catch[
  Do[
    Do[Print[{i, j}];
       If[j == 7, Throw[7]],
    {j, 10}],
  {i, 10}]]

(*    {1,1}    *)
(*    {1,2}    *)
(*    {1,3}    *)
(*    {1,4}    *)
(*    {1,5}    *)
(*    {1,6}    *)
(*    {1,7}    *)

(*    7    *)

The advantage of Throw over Break is that the latter quits only the innermost loop, whereas for the former you can decide how many loops to quit by setting the Catch appropriately.

Throw/Catch would probably work:

Catch[
  Do[
    Do[Print[{i, j}];
       If[j == 7, Throw[7]],
    {j, 10}],
  {i, 10}]]

(*    {1,1}    *)
(*    {1,2}    *)
(*    {1,3}    *)
(*    {1,4}    *)
(*    {1,5}    *)
(*    {1,6}    *)
(*    {1,7}    *)

(*    7    *)

The advantage of Throw over Break is that the latter quits only the innermost loop, whereas for the former you can decide how many loops to quit by setting the Catch appropriately.

Update: you seem to want to break only the outer loop but let the inner one finish the current iteration. This could work:

X = {{-10, 1, 1}, {8, 2, 2}, {-7, 3, 1}, {2, 4, 2}};
stop = False;
XX = Reap[Do[Do[If[X[[k, 1]] < 0, R = m*X[[k, 2]]; Sow[R];
                If[R == 9, stop = True], R = m*X[[k, 3]];
                Sow[R]]; If[R == 9, stop = True], {k, 1, 4}];
             If[stop, Break[]], {m, 1, 5}]][[2, 1]]
(*    {1, 2, 3, 2, 2, 4, 6, 4, 3, 6, 9, 6}    *)

The trick here is to set a flag stop in the inner loop, and at the end of the inner loop check this flag and conditionally break the outer loop.

Source Link
Roman
  • 49.8k
  • 2
  • 57
  • 131

Throw/Catch would probably work:

Catch[
  Do[
    Do[Print[{i, j}];
       If[j == 7, Throw[7]],
    {j, 10}],
  {i, 10}]]

(*    {1,1}    *)
(*    {1,2}    *)
(*    {1,3}    *)
(*    {1,4}    *)
(*    {1,5}    *)
(*    {1,6}    *)
(*    {1,7}    *)

(*    7    *)

The advantage of Throw over Break is that the latter quits only the innermost loop, whereas for the former you can decide how many loops to quit by setting the Catch appropriately.