Using operator OR in a Do-While loop (HELP!)
Paquicefalosaurio
I want to use an OR operator using a Do-While loop.
I'm trying this...
Variable/G Cont, A, NEnd
Wave WaveX
Wavestats WaveX
NEnd=V_npnts
do
if (WaveX[A]<=5)
Cont+=1
else
Cont=0
A+=1
endif
while (Cont<=100 || A<=NEnd)
Wave WaveX
Wavestats WaveX
NEnd=V_npnts
do
if (WaveX[A]<=5)
Cont+=1
else
Cont=0
A+=1
endif
while (Cont<=100 || A<=NEnd)
I'm trying to stop the loop when variable "Cont" reaches 100 or when "A" is larger than NEnd, however it doesn't work.
Sorry if this is very basic, but i'm starting using Igor, and also it is my first time using the OR command.
Please help!
Is there a typo in the while clause? you have A<=End, should it be A<=NEnd. Notice how End is color coded as a reserved word, blue.
Andy
June 28, 2016 at 08:25 pm - Permalink
Ups! My mistake. It should be NEnd, but still it doesn't work.
June 28, 2016 at 08:37 pm - Permalink
There is no "endif" to close out the if statement.
Andy
June 28, 2016 at 08:57 pm - Permalink
There is no "endif" to close out the if statement.
Andy
June 28, 2016 at 08:57 pm - Permalink
if (WaveX[A] <= 5)
endif
A+=1
while (A <= NEnd)
That code makes "out of range error".
"while" condition should be
June 28, 2016 at 10:33 pm - Permalink
while ((Cont<=100) || (A<=nEnd))
since '||' and '&&' have higher evaluation priority than '<=' or '=' ///// <- nope
HJ
June 29, 2016 at 09:15 am - Permalink
DisplayHelpTopic "Break Statement"
for a code example.
June 29, 2016 at 04:56 am - Permalink
Are you sure? I'm reading the operators help topic differently.
A quick example gives:
0
•print 1 < 0 || 1
1
June 29, 2016 at 06:14 am - Permalink
Exactly, it doesn't work. Finally, I tried this, but still it's doesn't working...
Wave WaveX
Wavestats WaveX
NEnd=V_npnts
do
if (WaveX[A]<=5)
Cont+=1
else
Cont=0
A+=1
endif
while ((Cont<=100) ||(A<NEnd))
June 29, 2016 at 06:51 am - Permalink
Back to topic:
A and Cont are not initialized properly, I think. In the first run it is set to zero but later on it is undefined. This might cause trouble but might be necessary as well; this depends on the remaining code.
Shouldn't the logical connection be AND instead of OR?
- Cont increases while A stays the same if wavex(a)<=5. Nothing else happens.
- Could you provide some information what should happen?
I would recommend to run the debugger and see what is happening with the variables.
displayhelptopic "the debugger"
NEnd might be also be assigned via the 'numpnts' or 'dimsize' commands. But this will not cause trouble here.
HJ
June 29, 2016 at 09:26 am - Permalink
Also, when you say "it doesn't work" it would be better to provide details. Does it give a compile error? A run-time error? If there is an error, what is the error message, and what line of code is highlighted? Perhaps it runs without error but gives an incorrect or unexpected result. Often in order for someone else to give help, you need to post complete runnable code, preferably cut down to something small that illustrates the puzzling behavior. Often, in creating a simple example of the problem, you will figure out what the problem is.
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
June 29, 2016 at 09:29 am - Permalink
Thank you everybody! Finally, i could fix it using an 'if()-break-endif' loop.
June 29, 2016 at 10:03 am - Permalink
Nema problema. The reason I asked was that I spent some hours a few weeks ago to understand an complicated expression without parantheses and realized that parantheses are in some cases not that bad at all.
June 29, 2016 at 12:20 pm - Permalink