Using operator OR in a Do-While loop (HELP!)

Hi!

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)


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!
Paquicefalosaurio wrote:
Hi!

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
while (Cont<=100 || A<=End)


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
hegedus wrote:
Paquicefalosaurio wrote:
Hi!

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
while (Cont<=100 || A<=End)


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


Ups! My mistake. It should be NEnd, but still it doesn't work.
Paquicefalosaurio wrote:
hegedus wrote:
Paquicefalosaurio wrote:
Hi!

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
while (Cont<=100 || A<=End)


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


Ups! My mistake. It should be NEnd, but still it doesn't work.


There is no "endif" to close out the if statement.

Andy
Paquicefalosaurio wrote:
hegedus wrote:
Paquicefalosaurio wrote:
Hi!

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
while (Cont<=100 || A<=End)


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


Ups! My mistake. It should be NEnd, but still it doesn't work.


There is no "endif" to close out the if statement.

Andy
Paquicefalosaurio wrote:
Hi!

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
while (Cont<=100 || A<=End)


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!


do
 if (WaveX[A] <= 5)
 endif
 A+=1
while (A <= NEnd)

That code makes "out of range error".

"while" condition should be
while (A < NEnd)


Besides the typos (end, endif), you need parentheses in the 'while' statement
while ((Cont<=100) || (A<=nEnd))
since '||' and '&&' have higher evaluation priority than '<=' or '=' ///// <- nope

HJ
I think a far less complicated approach would be to use an 'if()-break-endif' construct in the do-while loop. See
DisplayHelpTopic "Break Statement"
for a code example.
HJDrescher wrote:
'||' and '&&' have higher evaluation priority than '<=' or '='

Are you sure? I'm reading the operators help topic differently.
A quick example gives:
print 1 < (0 || 1)
  0
print 1 < 0 || 1
  1

thomas_braun wrote:
HJDrescher wrote:
'||' and '&&' have higher evaluation priority than '<=' or '='

Are you sure? I'm reading the operators help topic differently.
A quick example gives:
print 1 < (0 || 1)
  0
print 1 < 0 || 1
  1


Exactly, it doesn't work. Finally, I tried this, but still it's doesn't working...

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))


I am always (!) unsure about the priority of operands since they change from language to language (plus Igor distinguishes 'logical or' and 'bit-wise or' with different priority). That's why I often use 'unnecessary' parentheses to avoid misinterpretation. And it was late, sorry.

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
You might have a legitimate reason to use global variables, but it seems unlikely. You should avoid global variables like the plague if you can- they make bugs that are very difficult to debug because they create obscure connections and dependence on history that can be quite confusing.

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
s.r.chinn wrote:
I think a far less complicated approach would be to use an 'if()-break-endif' construct in the do-while loop. See
DisplayHelpTopic "Break Statement"
for a code example.


Thank you everybody! Finally, i could fix it using an 'if()-break-endif' loop.
HJDrescher wrote:
I am always (!) unsure about the priority of operands since they change from language to language (plus Igor distinguishes 'logical or' and 'bit-wise or' with different priority). That's why I often use 'unnecessary' parentheses to avoid misinterpretation. And it was late, sorry.


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.