LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how the While Loop really works

Hello!

I'm sorry if my question is stupid as I'm the beginner in LabView.

Question: how the LabView's While Loop looks if translated in text-based
programming language?

That's how I thought it looks:

do {
do something;
}
while ( stop condition is false );

Note: if stop condition is true before the while loop starts it will start
anyway and run at least one time. If the button STOP is pressed while "doing
something" the while loop stops.

and that's how it really looks (from my studying it):

exit = false;
do {
if ( stop condition is true ) then ( exit = true );

do something;
}
while ( exit is false );

Note: even if the button STOP is pressed while "doing something", the while loop
runs at least one more time!

And here's an example that proves the statement above:

When highlight mode is on, I see the following:
I: starting the VI;
LV: reading stop terminal (FALSE);
LV: waiting for timeout in event structure;
I: in the same time I'm pressing STOP button on front panel;
LV: displaying message (HELLO!);
LV: reading stop terminal (TRUE);
LV: waiting for timeout in event structure;
LV: displaying message (HELLO!);
LV: stopping VI.

LV (= LabView).

Do I really get it right: the while loop in LabView is not the same as while loop
with post-condition in text-based programming language?

Thanks in advance for any comments.

Notes:
1. The example was made up only for demonstration.
2. The event structure has no events but timeout event.
3. The front panel has only STOP button.
4. LabView 8.0 is used under WinXP.
Download All
0 Kudos
Message 1 of 7
(3,608 Views)
Rashid,

You are both right and wrong on this assumption.

The thing is that the reading of the "stop" terminal is just as affected by data flow as the rest of the code.  If you place the "stop" terminal at the end of a sequence, it will get read at the end.  If you leave it "in the wild" it'll get read whenever LabVIEW feels like it.

So basically, the While loop in LAbVIEW can be used like a while loop in conventional languages except that you have the power of control to determine WHEN the stop criterium will be evaluated.

Hope this helps

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 2 of 7
(3,572 Views)
Hello Rashid,
basically the difference is the following: in a text based language the stop condition is always evaluated after the "something" is done, while in LV the stop condition evaluation may be part of the "something"!
Unless you accurately serialize the code using dataflow or sequence structures, the Stop button can be read before, during or after the rest of the code contained in the loop: it is not a separate code item. So, when you click on Stop, it is likely that the button value has already been read, thus its true value will be good for the next loop.

Paolo
Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 3 of 7
(3,570 Views)
Unlike text languages which are sequential, LabVIEW uses dataflow.  This is what is causing you to see what you are seeing.

The terminator terminal will have data available when the while loop is first begun, so it will be evaluated first.  To cause it to run later, put the stop button's control terminal in the last sequence of your sequence structure.  This will cause LabVIEW to wait until the event structure has timed out before it looks at the stop's control terminal for data. 

In dataflow, if the data is available for evaluation, it will evaluate at that time.  There are two parallel processes in your loop, the evaluation of the front panel control and the sequence.  The sequence takes longer so the evaluation of the stop button occurs first.  The loop itself does not end until everything in the loop has been run.  If the evaluation of the front panel control (which occured first) told the loop to run again, even if it is later changed, the loop will run again.

Hope that this helps,
Bob Young

0 Kudos
Message 4 of 7
(3,566 Views)
Here is a bether way to make the reading of you Stop button.
Keep in mind to develop in state machine. it will help you in time critical application an on complex application.
Remember to look when the control are readed.
 
Benoit
Benoit Séguin
Software Designer
0 Kudos
Message 5 of 7
(3,557 Views)

Rashid,

You've already received some great comments. It is all in the dataflow!

On a related note: Long ago, we had a discussion on how to implement a while loop that acts as if it runs zero times (instead of at least once) if the stop condition is true from the beginning. Read all about it HERE.

 

0 Kudos
Message 6 of 7
(3,537 Views)
Thanks to all for your help.
I really appreciate it.

If I get it right, the while loop equivalent is the following:

stop condition = false;
do {
stop condition = stop button value; // reading front panel
do something; // running event structure, displaying msg
}
while ( stop condition is false );

It's almost the same as the previous scheme with exit flag
I used in my first message, but now I see my fault:

reading stop button is not checking stop condition.
reading stop button is like doing something,
one of activities in the while loop.
and checking stop condition is implicit and is done by LabView
inside terminator terminal.

Thanks to all once again.
Good luck!

Message Edited by Rashid on 01-18-2006 11:08 AM

0 Kudos
Message 7 of 7
(3,529 Views)