LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For-Loops: The question no one asks.

Why don't For-Loops have a premature exit capability --- i.e. why didn't
NI include the exit capability that exists in other languages? Is it a dataflow
thing? I don't see how. Just curious.
0 Kudos
Message 1 of 11
(4,286 Views)
Jay,

An easy way of implementing what you want is to use a while loop instead of a formal for loop. You can implement the functionality of a for loop with a while loop by comparing the value at the iteration terminal in the while loop with the number of times you want the loop to execute (minus 1). The output of this compare can be used to terminate the while loop. You then use logic gates to provide an OR function that terminates the while loop either when the final count is reached OR when some other event occurs (early termination signal).
Message 2 of 11
(4,288 Views)
They did provide the capability. It's called a while loop.

"Jay" wrote:
>> Why don't For-Loops have a premature exit capability --- i.e. why didn't>NI
include the exit capability that exists in other languages? Is it a dataflow>thing?
I don't see how. Just curious.
Message 3 of 11
(4,288 Views)
"Dennis Knutson" wrote:
>>They did provide the capability. It's called a while loop.>>"Jay"
wrote:>>> Why don't For-Loops have a premature exit capability --- i.e.
why didn't>NI>include the exit capability that exists in other languages?
Is it a dataflow>thing?> I don't see how. Just curious.


Come now, Dennis. Everyone knows that While-Loops are just a work-around
for the fact that NI chose not to include a real exit function / capability
in For-Loops. Other languages have such a capability. Why not LabView?

Jay
0 Kudos
Message 4 of 11
(4,288 Views)
I believe that the loop exit capability wasn't included because it was considered bad
programming practice, like using goto's.

Tim

Jay wrote:

> "Dennis Knutson" wrote:
> >>They did provide the capability. It's called a while loop.>>"Jay"
> wrote:>>> Why don't For-Loops have a premature exit capability --- i.e.
> why didn't>NI>include the exit capability that exists in other languages?
> Is it a dataflow>thing?> I don't see how. Just curious.
>
> Come now, Dennis. Everyone knows that While-Loops are just a work-around
> for the fact that NI chose not to include a real exit function / capability
> in For-Loops. Other languages have such a capability. Why not LabView?
>
> Jay
0 Kudos
Message 8 of 11
(4,288 Views)
Hey, Tim. You say:

>I believe that the loop exit capability wasn't included because it was considered
bad programming practice, like using goto's.

That's funny. How can it be any worse (or different) than putting exit
logic into a While-Loop? As for Goto's, they're the basic logic behind the
much revered State Machine, and no one seems to mind that. If you're not
careful, State Machines can wind up producing some pretty good spaghetti
code, just like any text-based code can do.

Jay

>> Original message: Why don't For-Loops have a premature exit capability
--- i.e. why didn't NI include the exit capability that exists in other languages?
Is it a dataflow thing? I don't see how. Just curious.
0 Kudos
Message 9 of 11
(4,288 Views)
I don't think a LabVIEW loop is just a wrapper around a C-code loop - don't forget
that when you have parallel loops in a diagram, the LabVIEW execution system will
time-share the activity inside each loop. So I think of a LabVIEW loop more as
spawning a Thread object. Could a for-loop be re-written to have a conditional
terminal? Sure, why not, it's programming and you can get anything to do what you
want. But it's like having an iteration terminal starting at something other than
zero - it's just extra baggage that usually isn't needed, so instead use a plus object
when you do need it.

-Vinny Recca

Jay wrote:

> Hey, Tim. You say:
>
> >I believe that the loop exit capability wasn't included because it was considered
> bad
programming practice, like using goto's.
>
> That's funny. How can it be any worse (or different) than putting exit
> logic into a While-Loop? As for Goto's, they're the basic logic behind the
> much revered State Machine, and no one seems to mind that. If you're not
> careful, State Machines can wind up producing some pretty good spaghetti
> code, just like any text-based code can do.
>
> Jay
>
> >> Original message: Why don't For-Loops have a premature exit capability
> --- i.e. why didn't NI include the exit capability that exists in other languages?
> Is it a dataflow thing? I don't see how. Just curious.
0 Kudos
Message 10 of 11
(4,288 Views)
Jay,

I have to say I`m no programming Guru, but isn`t a State Maschine based on a
case (Switch) statement? This statement does allow the user to hop to and
fro within a certian pre-defined space, but the use of a GOTO is, as far as
I am aware, not limited to a particular region of the code. At least with a
switch statement (Case) the beginning point of each "choice" is well defined
and fixed, therefore reducing the change of mistakes.

just my 2 cents

Shane

Jay schrieb in Nachricht <3a919621@newsgroups.ni.com>...
>
>Hey, Tim. You say:
>
>>I believe that the loop exit capability wasn't included because it was
considered
>bad programming practice, like using goto's.
>
> That's funny. How can it be any worse (or different) than putt
ing exit
>logic into a While-Loop? As for Goto's, they're the basic logic behind the
>much revered State Machine, and no one seems to mind that. If you're not
>careful, State Machines can wind up producing some pretty good spaghetti
>code, just like any text-based code can do.
>
>Jay
>
>>> Original message: Why don't For-Loops have a premature exit capability
>--- i.e. why didn't NI include the exit capability that exists in other
languages?
> Is it a dataflow thing? I don't see how. Just curious.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 11 of 11
(3,986 Views)
I don't think there is an "exit" or "continue" feature in LabVIEW like we might find in C or other languges. This may have to do with the difference between dataflow and other types of languages.

Even in C if you think of the operation of a for loop it really works like a while loop.

for (initialization;condition;per loop)
{
// Loop
}

In C all that happends is that the loop continues to execute until the "condition" is false ... that could be based on an increment but it could be based on any logical operation.

Guess it comes down to the differences in how languages are implemented, like some languages providing while loops that will always execute once as well as one that may not execute at all ... while others only give one option.

Guess this still doesn't answ
er the questions on why the ability to exit a for loop isn't there but thought I'd all my 0.2 cents.

I'll pass the suggestion on to the Application Engineers at NI as a feature suggestion for LabVIEW.

Cheers,
Kamran
An
0 Kudos
Message 5 of 11
(4,288 Views)
Thank you for your thoughtful response. You point out, in part, that:

... Even in C if you think of the operation of a for loop it really works
like a while loop.

for (initialization;condition;per loop)
{
// Loop
}

Not only is the exit capability missing in LabView, so is the "per loop"
feature. Even lowly BASIC provides that capability. It, for example, has
the capability to increment the loop index by a multiple factor (e.g. i =
0, 2, 4, ...). Additionally, you don't even have to start the index at 0.
You can increment the loop, for example, from -n to +m in steps of k.

As you indicate, LabView's underlying C-code --- if in fact it is the
underlying code --- is not nearly as restrictive as LabView currently is.
LabView certainly has some growing
room.

Jay
0 Kudos
Message 6 of 11
(4,288 Views)