Re: "break" equivalent in Macro language.
Posted by
Michael Ellis on
Dec 07, 2010; 3:09pm
URL: http://imagej.273.s1.nabble.com/break-equivalent-in-Macro-language-tp3686262p3686266.html
Dear Ved, your code and my code differ. Look carefully at the "if/else" statement in my example:
a = 0;
b = 0;
var done = false; // used to prematurely terminate loop
for (i=0;i<10 && !done;i++) {
// code 1
print("a is " + a++);
if (a==3) {
done = true; // break
} else {
// code 2
print("b is " + b++);
}
}
// code 3
When a is equal to 3 done is set to true. code 2 is inside the else branch of the conditional statement and will NOT be executed when a == 3.
code 3 will be executed.
Try pasting the example into the Image macro and executing it. The output you should see in the log window is
a is 0
b is 0
a is 1
b is 1
a is 2
Regards -- Michael Ellis
On 7 Dec 2010, at 14: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