From bug-octave-request at che dot utexas dot edu Wed Aug 17 09:55:15 1994 Subject: Octave "break" bug ? From: "R.D. Auchterlounie" To: bug-octave at che dot utexas dot edu Date: Wed, 17 Aug 1994 15:55:04 BST 1; % -*- Octave script file -*- % % Octave bug report. % ------------------ % % Summary: "break" sometimes breaks out of more than one level of % nested for loops. % % Reproducing the bug: Save this message to a script file and execute it. % % Info: Octave version: 1.0 % Machine: AMD486DX40 PC % OS: Linux v1.0 % Configuration: Binary distribution - with no modifications % to octave source according to accompanying % information file. % From: rda at eng dot cam dot ac dot uk % % Detail: % % This problem occurred in a program of mine which has several nested % for loops. The limits on the loop range for the inner loop are % variable, in some cases the inner loop may only be a single % iteration. If a certain condition occurred I wished to terminate the % innermost loop - which should be achievable with a "break" % statement. Unfortunately using "break" lead to errors which I have % traced to the break statement breaking out of TWO levels of loop. % % The following function is a simple demonstration of the bug, it % contains 3 levels of "for" loops. The outer loop is two iterations, % the middle loop is 3 (1:3). The inner loop should terminate after 1 % iteration (only) when the middle loop variable is 2. The range % limits for the inner loop are passed as the two function arguments. % % The use of fprintf "'" and "end" allow the function to be run unchanged % by Matlab4. % function loop(a,b); fprintf(1,'\n*** loop called with %d,%d\n',a,b) for l=1:2 fprintf(1,'---------------------------- outer loop %d\n',l) for m=1:3 fprintf(1,'------------- middle loop %d\n',m) for n=a:b fprintf(1,'inner loop %d\n',n); if (m==2) % as far as I can see this should terminate (only) the inner % for loop after the first iteration, on the second iteration % of the outer loop. fprintf(1,'break inner\n'); break; end ; % if end ; % inner for end ; % middle for end ; % outer for end ; % function % % % When the function is called with unequal arguments, eg: % loop(1,2) % % we get the expected results - twice round the outer loop, three % times round the middle loop and a variable number of times round the % inner loop. This also works for zero inner loop iterations, eg: % loop(2,1) % % But when I want one inner loop iteration, eg: % loop(1,1) % % The _middle_ loop is broken after the break is executed in the inner % loop. When the same function is tested in Matlab the middle loop is % not broken. % % Thanks for any help. % ray