While not executing

Help… I’m trying to figure out why a snippet of code inside of a Whle…Wend will not execute.

The While looks like the following:

while (nYROD = 1 and nAFYD > 0) or nBUUCOST > 0

and the parameters are as follows according to the debugger:

nYrod = 1
nAFYD = 0
nBUUCOST = 2419.25

The fact that the nBUUCOST is greater than zero means it should execute. It skips over the entire While…Wend code.

If I replace the While with an “If” it will run it one time.

I believe this code has executed in past versions without any problems. I’m using release 2014r3.2.

Stumped beyond words…

[quote=163911:@John Fatte]Help… I’m trying to figure out why a snippet of code inside of a Whle…Wend will not execute.

The While looks like the following:

while (nYROD = 1 and nAFYD > 0) or nBUUCOST > 0

and the parameters are as follows according to the debugger:

nYrod = 1
nAFYD = 0
nBUUCOST = 2419.25

The fact that the nBUUCOST is greater than zero means it should execute. It skips over the entire While…Wend code.

If I replace the While with an “If” it will run it one time.

I believe this code has executed in past versions without any problems. I’m using release 2014r3.2.

Stumped beyond words…[/quote]

You want to set your variable values before you start the while/wend. Otherwise the condition is not true when you start, and the loop never happens.

Or use a do loop which is evaluated at the end :

do nYrod = 1 nAFYD = 0 nBUUCOST = 2419.25 loop until (nYROD <> 1 and nAFYD <= 0) or nBUUCOST <= 0

the while condition includes

and nAFYD > 0

since

nYrod = 1
nAFYD = 0
nBUUCOST = 2419.25

then that part is not going to be true.

I agree that nBUUCOST > 0 SHOULD mean it fires.
However, I am sure that there is some optimised code in place in Xojo that evaluates booleans left to right, and makes a decision based on the first value.

Try wrapping it all up in brackets

while ( (nYROD = 1 and nAFYD > 0) or nBUUCOST > 0)

and try putting the result into a variable , then using the variable

eg

[code]dim Testcondition as boolean

Testcondition = nBUUCOST > 0
testcondition = testcondition or (nYROD = 1 and nAFYD > 0)

while testcondition
//stuff

Testcondition = nBUUCOST > 0
testcondition = testcondition or (nYROD = 1 and nAFYD > 0)
wend[/code]

It is true that Xojo optimizes such statements so that it evaluates the minimum number of conditions to determine the outcome. Where there are Ors involved, as above, the first set of conditions being false means that the second set would be evaluated. If it were an And, the first set being false would stop the evaluation.

John, at what point are you determining the values of your variables? Before it hits the While loop? Are you sure it’s even getting to the While loop?

The real stumper is why changing to an If works. Try rewriting your code like this just to see what it’s seeing:

dim condition1 as boolean = nYROD = 1
dim condition2 as boolean = nAFYD > 0
dim condition3 as boolean = nBUUCOST > 0

while ( condition1 and condition2 ) or condition3

Also, how did you dim those variables? What are their types?

[quote=163921:@Kem Tekinay]It is true that Xojo optimizes such statements so that it evaluates the minimum number of conditions to determine the outcome. Where there are Ors involved, as above, the first set of conditions being false means that the second set would be evaluated. If it were an And, the first set being false would stop the evaluation.

John, at what point are you determining the values of your variables? Before it hits the While loop? Are you sure it’s even getting to the While loop?

The real stumper is why changing to an If works. Try rewriting your code like this just to see what it’s seeing:

dim condition1 as boolean = nYROD = 1
dim condition2 as boolean = nAFYD > 0
dim condition3 as boolean = nBUUCOST > 0

while ( condition1 and condition2 ) or condition3

Also, how did you dim those variables? What are their types?[/quote]

I simply added this before his code and the while happens :

nYrod = 1 nAFYD = 0 nBUUCOST = 2419.25

All the variables are initialized before it gets to the WHILE. I verified this in the debugger BEFORE it gets to the loop.

Kem, yes, if I substitute an IF in place of the WHILE, it executes just fine, but only one time.

I tried surrounding the parameters in the WHILE with parentheses like this: ((nYROD = 1 and nAFYD = 0) OR nBUUCOST > 0)
but it still skips over it.

I tried this:

dim DOLOOP As Boolean = (nYROD = 1 and nAFYD = 0) OR nBUUCOST > 0

DOLOOP was then TRUE

Changed the WHILE to:

WHILE DOLOOP

Guess what, it still skips over it.

What I have resorted to in the mean time is the following:

if (nYROD = 1 and nAFYD = 0) OR nBUUCOST > 0 then

for Zebra as Integer = 1 to 100 // WILL NEVER GET TO 100 BASED ON EXECUTING CODE

NEXT
end

I can now get the code to execute, still don’t see why the WHILE will not execute.

I have to believe that there is some sort of missing “end” or parentheses that the compiler is not catching.

nYROD = INTEGER
nAFYD = DOUBLE
nBUUCOST = DOUBLE

All properly initialized and used many times throughout the code without any problems. If one of these was not initialized properly there would be many issues of incorrect calculations.

This executes fine and the break happens :

[code] dim nYrod as integer = 1
dim nAFYD as double = 0
dim nBUUCOST as double = 2419.25

while (nYROD = 1 and nAFYD > 0) or nBUUCOST > 0
break
nYrod = 1
nAFYD = 0
nBUUCOST = 2419.25
wend
[/code]

Why does it skip for you is one of these mysteries … Maybe you like it that way ?

John can you share your source code? I’d like to figure this out.

This almost sounds like a stale / corrupted compilation cache - I’d quit the IDE, nuke all Xojo Caches, then try again.

Kem, I’d really like to share the code, but this is a commercial application that prevents me from doing that.

This particular code segment is hundreds of lines long and not just as simple as some who have replied might think. I know it’s something strange in this code segment because if I just put the following code before the actual “While” code, it skips over it as well.

While ((nYROD = 1 and nAFYD > 0) OR nBUUCOST > 0)
exit
Wend

This is a module that is called from a window so there is no possibility of a variable and a property that might be the same name. There are 250 lines of code that are executed before it even gets to this “While” statement. Here is the variable declaration that includes the variables used in the “While” statement. Of course, some of these values will change as it runs through the code, but as I stated, I interrogated the values in the debug just before it got to the “While” and they should allow the While to execute, but it does not.

dim ACQ As Date = rsASSETS.Field(“ACQ”).DateValue
dim nCOST As Double = 0.00
dim nS179 As Double = 0.00
dim cITCS As String = “”
dim nITCA As Double = 0.00
dim nSALV As Double = 0.00
dim nYROD As Integer = 1
dim BONUSTAKEN As Boolean = False
dim nBONUSPCT As Double = 0.00
dim nAFYD As Double = 0.00
dim nBPCT As Double = 0.00
dim cLISTED As String = “”
dim nBASE As Double = 0.00
dim VBUP As Boolean = False
dim nBUUCOST As Double = 0.00
dim nACTUALBPCT As Double = 0.00
dim cSTATUS As String = “”
dim cSTATUS_SAVE As String = rsASSETS.Field(“STATUS”).StringValue
dim nYROD_SAVE As Integer = rsASSETS.Field(“YROD”).IntegerValue
dim nACCD As Double = 0.00
dim nMTYR As Double = 0.00
dim nNXYR As Double = 0.00 // NEXT YEAR’S DEPR

I’m going to try Michael’s suggestion and see if I can clear the cache. I’m running on a Mac, but will test on Windows to see if I get the same behavior and report back.

As a quick test, can you try temporarily putting the While statement at the top of your code, just after your variable declarations?

Gavin

Nice suggestion and one that I thought of before. Here is the code entered directly after the variable declaration:

nBUUCOST = 2419.25
while ((nYROD = 1 and nAFYD > 0) or nBUUCOST > 0)
MsgBox “hello”
Exit
Wend

Note that I had to change the nBUUCOST to a value greater than 0 or it wouldn’t get into the while.

Even after doing that, it still does not execute the msgbox in the “while”, just jumps to the Wend.

I also tried this:

nAFYD = 15
while ((nYROD = 1 and nAFYD > 0) or nBUUCOST > 0)
MsgBox “hello”
Exit
Wend

after the variable assignment, it jumps to the Wend.

Very strange!

I know how bizarre this has to sound, but I’ve been working in Xojo for over 4 years and have a number of applications developed in it so I’m not a novice when it comes to this kind of stuff. I put a message box just before the while to verify that the values shown in the debugger are valid and they are as they should be.

I’ve already found a work-around so this is not holding anything up, but if there is something in the code that is causing this, I want to know what it is so I can determine if it’s me or Xojo.

What happens if you write it like this instead:

while (nYROD = 1 and nAFYD > 0.0) or nBUUCOST > 0.0

Kem, I was just about to press the Reply button when your reply with the same question appeared. In my 55+ years of programming I have seen oddities corrected by things like you just suggested. Once had a PL/I program that would abend half way through saying that a variable had no value. I could put a trace on that variable and it had a value right up to the statement that would fail. After an agonizing couple of days I just did a global rename on the variable and the program then worked. The original name was not in the list of restricted names, no where near a restricted name. Go figure.

Kem

I assume you mean with the implicit decimal points? I’ll try it right now. I can verify that I get the same behavior in Windows. Skips over the While.

Testing right now, Xojo just crashed so I have to do over… back in a minute.

Boy do I feel like an idiot.

OK, so I thought you were on to something because my simplified test worked with the explicit decimals points. But then I decided to go back to my original code and found that when Xojo is running through a While loop, it jumps to the Wend FIRST, then cycles back up to the line of code AFTER the “While” statement. I guess I wasn’t tracking it through far enough to see this behavior because when it skipped all the code inside the “While” and jumped to the Wend, I thought it was completely skipping that code. It wasn’t.

Problem solved… chalk this up to user error ( and stupidity ).

Thanks for everyone’s feedback… Obviously, no one has actually watched a While execute like I was or they may have said, hey, keep stepping through the code and it will cycle back.

Actually, that had occurred to me, but since you said

:smiley:

Don’t worry about it, we’ve all been there. Except me, of course.

Actually, I didn’t know it did that although I rarely use while…wend. So maybe we all learned something today. Except Kem, of course.

Thanks to all who responded and shared in my learning experience.