Login  Register

Re: "break" equivalent in Macro language.

Posted by Michael Schmid on Dec 07, 2010; 4:09pm
URL: http://imagej.273.s1.nabble.com/break-equivalent-in-Macro-language-tp3686262p3686265.html

Hi Ved,

yes, it is not a perfect equivalent of break. You would need an  
'else' clause:

a=0;
for (i=0; i<100; i++) {
   a++;
   if (a==3) i=1e99; //break if this condition is fulfilled
   else { //put everything after 'break' here
     print ("code2 after break: a="+a);
   }
}

Of course, the solution of Michael Ellis with 'i<100 & !done' in the  
'for' statement may be considered more elegant.

Michael
________________________________________________________________

On 7 Dec 2010, at 15:53, Ved Sharma wrote:

> a=0;
> for (i=0;i<10;i++) {
>    code1;
>    a++;
>    if (a==3) break;
>    code2;
> }
> code3;
>
>
> In the above example, I want to break as soon as a==3, so that it  
> does not execute code2. It should terminate for loop and go on to  
> execute code3.
>
> Michael, setting i to a very large value or using a variable done  
> (as in your example) still evaluates the condition i<10, which  
> causes code2 to run.
>
> Ved