Hello,
As most of you already know that "break" statement is used in JAVA to exit a loop (e.g. a for loop). It seems to me that it is not recognized in ImageJ's Macro language. Is there some other statement/code I can use in my macro in place of "break"? Ved |
Hi Ved,
it is easy to break a loop without a break statement, e.g.: for (i=0, i<100; i++) { do something, set 'end_reached' if we should break if (end_reached) i = 1e99; //break } Michael ________________________________________________________________ On 7 Dec 2010, at 05:24, Ved Sharma wrote: > Hello, > > As most of you already know that "break" statement is used in JAVA > to exit a loop (e.g. a for loop). It seems to me that it is not > recognized in ImageJ's Macro language. Is there some other > statement/code I can use in my macro in place of "break"? > > Ved |
In reply to this post by ved sharma
Hi Michael,
Thanks for your reply but I don't understand how you "set end_reached". How would you do that for this example code, so that the last iteration of print(b++) is not executed. a = 0; b = 0; for (i=0;i<10;i++) { print(a++); if (a==3) i = 1e99; print(b++); } Ved |
Setting i to 1e99 (a very large value much greater than 10 is what causes the loop to terminate when the next executes the conditional test i < 10.
On 7 Dec 2010, at 14:07, Ved Sharma wrote: > a = 0; > b = 0; > for (i=0;i<10;i++) { > print(a++); > if (a==3) i = 1e99; > print(b++); > } You can also use a separate boolean variable, lets call it "done" that can be used to prematurely terminate the loop. You can use an "if" statement to to avoid the remainder of the loop being executed: a = 0; b = 0; var done = false; // used to prematurely terminate loop for (i=0;i<10 && !done;i++) { print("a is " + a++); if (a==3) { done = true; // break } else { print("b is " + b++); } } There are many variations on this theme that you can use. Regards -- Michael Ellis |
In reply to this post by ved sharma
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 |
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 |
In reply to this post by ved sharma
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 |
In reply to this post by ved sharma
Thanks Michael Ellis and Michael Schmid! It does the job.
Ved |
Free forum by Nabble | Edit this page |