Reputation: 528
In a couple of weeks I will be joining a project that (currently) uses LabView for development. To get myself at least somewhat familiar before this happens I have been creating some simple projects in the trial version of the software. Someone challenged me to write a simple program that could preform simple division without using the division operator.
I've written the program successfully, but my main while
loop seems to run one too many times. Here is my program:
The user inputs a dividend and divisor and the program will continually subtract the divisor from the dividend until the dividend becomes <= 0, at which point it should break. The program runs successfully, but when the loop finally stops the dividend always equals x below 0 (where x is whatever number the divisor is). While debugging the application I found the problem, when the loop comparison happens for the final time the dividend will equal 0 and evaluate to 'false' however the code inside the loops executes one last time before the loop breaks. This is what I would expect from a do-while loop, but not a simple while.
Just to prove to myself that it wasn't an (hopefully) obvious logic error I wrote (what I consider to be) an equivalent program in python that does exactly what I expect.
I've spent a long time googling, staring at it, I even did it on paper but I cannot figure out why it doesn't do what I expect. What gives?
Upvotes: 5
Views: 2671
Reputation: 182
The Python code you've written is not equivalent. A while loop in LabVIEW is actually a do while loop, the code it contains will always execute at least once. This also means that the condition isn't evaluated until after the code it contains has executed.
Upvotes: 0
Reputation: 723
LabVIEW executes it's code according to the dataflow principle Which means that the loop cannot stop, until it has finished executing all the code inside it. This is the NI document confirming the above (see the very first flowchart). Moreover, subtraction and comparison are happening simultaneously.
The code you have is largely equivalent to (except that comparison with 0 happens on a temporary value in the wire) :
dividend = YYY
divisor = XXX
dividend = dividend - divisor
while dividend > 0:
dividend = dividend - divisor
If you'll be really getting into LabVIEW I strongly suggest you avoid using local variables. Many times (including this one) they are bad. Do it like this instead:
This is a snipplet, so if you drag this file from explorer and drop it onto your BD it will appear as a piece of code (LV2014).
Upvotes: 3
Reputation: 4628
I believe that the evaluation of the condition and the subtraction happens in parallel as opposed to after each other that's why you always get one more subtraction than you need.
Edit
As it's told in the dataflow tutorial (Figure 2) any operation as soon as all inputs are available can be expected to be executed. You can not know and should not rely on the order of execution for operations that are ready to be performed.
Upvotes: 1