BREAK statement [message #91343] |
Wed, 01 July 2015 08:41  |
g.nacarts
Messages: 148 Registered: November 2013
|
Senior Member |
|
|
Hi I typed the following:
A = sqrt(Ax^2 + Ay^2)
nozero = where(A ne 0, cnt)
if cnt eq 0 then break
I got the following error:
BREAK must be enclosed within a loop (FOR, WHILE, REPEAT), CASE, or SWITCH statement.
The error is very clear. I am not sure if the following is equivalent
A = sqrt(Ax^2 + Ay^2)
nozero = where(A ne 0, cnt)
if cnt eq 0 then return
Using the return instead of break because that part of code does not enclosed within a loop. If not so, can anyone suggest a statement doing the same thing as BREAK without being enclosed within a loop?
|
|
|
Re: BREAK statement [message #91344 is a reply to message #91343] |
Wed, 01 July 2015 08:55   |
Helder Marchetto
Messages: 520 Registered: November 2011
|
Senior Member |
|
|
On Wednesday, July 1, 2015 at 5:41:35 PM UTC+2, g.na...@gmail.com wrote:
> Hi I typed the following:
>
> A = sqrt(Ax^2 + Ay^2)
>
> nozero = where(A ne 0, cnt)
> if cnt eq 0 then break
>
> I got the following error:
> BREAK must be enclosed within a loop (FOR, WHILE, REPEAT), CASE, or SWITCH statement.
>
> The error is very clear. I am not sure if the following is equivalent
>
> A = sqrt(Ax^2 + Ay^2)
>
> nozero = where(A ne 0, cnt)
> if cnt eq 0 then return
>
> Using the return instead of break because that part of code does not enclosed within a loop. If not so, can anyone suggest a statement doing the same thing as BREAK without being enclosed within a loop?
Break and Return are simply different.
From the help
Break: exits a loop (as said in the error code)
Return: causes the program context to revert to the next-higher program level
In other words:
pro runTest
print, 'start test'
testBreakReturn
print, 'end test'
end
Pro testBreakReturn
for i=0,10 do begin
print, 'counting ',i
if i eq 5 then break
endfor
print, "I'm out of the for loop!"
print, 'Now I will call the return statement'
return
print, 'this line will not be executed becuase the return makes me return to the previous pro!'
end
I hope it is clear.
Look up:
http://www.exelisvis.com/docs/BREAK.html
http://www.exelisvis.com/docs/RETURN.html
Cheers,
Helder
|
|
|
Re: BREAK statement [message #91345 is a reply to message #91344] |
Wed, 01 July 2015 10:17   |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Wednesday, July 1, 2015 at 9:55:12 AM UTC-6, Helder wrote:
> On Wednesday, July 1, 2015 at 5:41:35 PM UTC+2, g.na...@gmail.com wrote:
>> Hi I typed the following:
>>
>> A = sqrt(Ax^2 + Ay^2)
>>
>> nozero = where(A ne 0, cnt)
>> if cnt eq 0 then break
>>
>> I got the following error:
>> BREAK must be enclosed within a loop (FOR, WHILE, REPEAT), CASE, or SWITCH statement.
>>
>> The error is very clear. I am not sure if the following is equivalent
>>
>> A = sqrt(Ax^2 + Ay^2)
>>
>> nozero = where(A ne 0, cnt)
>> if cnt eq 0 then return
>>
>> Using the return instead of break because that part of code does not enclosed within a loop. If not so, can anyone suggest a statement doing the same thing as BREAK without being enclosed within a loop?
>
> Break and Return are simply different.
> From the help
>
> Break: exits a loop (as said in the error code)
> Return: causes the program context to revert to the next-higher program level
>
> In other words:
>
> pro runTest
> print, 'start test'
> testBreakReturn
> print, 'end test'
> end
>
> Pro testBreakReturn
> for i=0,10 do begin
> print, 'counting ',i
> if i eq 5 then break
> endfor
> print, "I'm out of the for loop!"
> print, 'Now I will call the return statement'
> return
> print, 'this line will not be executed becuase the return makes me return to the previous pro!'
> end
>
> I hope it is clear.
>
> Look up:
> http://www.exelisvis.com/docs/BREAK.html
> http://www.exelisvis.com/docs/RETURN.html
>
> Cheers,
> Helder
Maybe you want to use "STOP" to actually halt execution?
-Chris
|
|
|
Re: BREAK statement [message #91358 is a reply to message #91345] |
Fri, 03 July 2015 07:07   |
g.nacarts
Messages: 148 Registered: November 2013
|
Senior Member |
|
|
Thanks Helder, your example it makes sense but still something confuses me.
I had the following procedure
PRO TEST
while iter_lim_count lt iter_lim do begin
for m=0L,nB-1 do begin
for n=0L,nB-1 do begin
A[m,n]+=X
tmp =
B[m,n] = (tmp - init)/X
A[m,n]-=X
endfor
endfor
while tmp GT init do BEGIN
PRINT,'WHILE LOOP'
beta_test = beta_test*2.
A_test = B - beta_test*B
if beta_test LT 0.5 then return
ENDWHILE
PRINT,'EXIT WHILE LOOP'
A = A_test
iter_lim_count = iter_lim_count+1
endwhile
END
The only thing I've changed is to split the whole procedure into two parts and call them in the TEST pro.
PRO TESTING_A
for m=0L,nB-1 do begin
for n=0L,nB-1 do begin
A[m,n]+=X
tmp =
B[m,n] = (tmp - init)/X
A[m,n]-=X
endfor
endfor
END
PRO TESTING_beta
while tmp GT init do BEGIN
PRINT,'WHILE LOOP'
beta_test = beta_test*2.
A_test = B - beta_test*B
if beta_test LT 0.1 then return
ENDWHILE
PRINT,'EXIT WHILE LOOP'
A = A_test
END
PRO TEST
while iter_lim_count lt iter_lim do begin
TESTING_A
TESTING_beta
iter_lim_count = iter_lim_count+1
endwhile
END
I noticed that in the TESTING_beta pro the 'WHILE LOOP' was printing but the 'EXIT WHILE LOOP' never prints out. I am confused because before that I had everything in one function both 'WHILE LOOP' and 'EXIT WHILE LOOP' are printed out. Why now the 'EXIT WHILE LOOP' does not printed out?
|
|
|
|
Re: BREAK statement [message #91361 is a reply to message #91360] |
Fri, 03 July 2015 08:57  |
Helder Marchetto
Messages: 520 Registered: November 2011
|
Senior Member |
|
|
On Friday, July 3, 2015 at 5:31:25 PM UTC+2, g.na...@gmail.com wrote:
> If the problem is the way that I passed the variables then why when I used the BREAK instead of RETURN the 'EXIT WHILE LOOP' printed? I am not saying that is the same thing obviously is doing something different but the A_test was there. That's why I get confused.
Yes, you're for sure confused.
break exits the while loop and then the print "exit while loop" is printed. Return exits the procedure.
I don't think you will find someone willing to understand your problem and explain the basics of programming. It's Friday afternoon, at least here, and the weekend has either already started or is going to start pretty soon. There's no better time to get your hands on a programming book or tutorial and see how all these loops, procedures and functions work together.
Good luck.
Cheers,
Helder
|
|
|