Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Unexpected behavior

Apr
322
7
Friends and collegues,

The code below speaks for itself,I think. I would expect both forms to work.
Ideas anyone?

Thanks, DJ.



:: First form - does not work

echos The batchfile was called with ``
if %# eq 0 (
echo no arguments.
) else (
echo %# arguments.
do i = 1 to %#
echo %i: %[%i]
enddo
)



:: Second form - does work

echos The batchfile was called with ``
if %# eq 0 (
echo no arguments.
quit
)

echo %# arguments.
do i = 1 to %#
echo %i: %[%i]
enddo

quit
::EOF
 
The problem is multiple commands after "else." A DO loop apparently isn't compatible with that because the DO loop contains multiple commands itself.

A simple else statement
else (echo %# arguments. & echo nonsense.)​
works fine, but don't forget the command separator (&).

Why use if? It's much less preferred when iff is available. Re-write the first form with iff and it works just fine.

echos The batchfile was called with ``
iff %# eq 0 then echo no arguments.
else
echo %# arguments.
do i = 1 to %#
echo %i: %[%i]
enddo
endiff
 
Last edited:
Thanks,
You know I knew that. Why use if? Why the x not?
What I did certainly not know is "iff is much preferred over if"? Does is actually say that somewhere in the docs?
Is there a performance difference that anyone knows of?
Cheers,
DJ
 
The IF / ELSE statement syntax you're using (from CMD) requires everything to be evaluated as a single line (it's assembled from your command groups to a single line before execution), and you're not using the single-line DO syntax to allow that.

IFF doesn't put everything into a single line before execution. Since you're not using CMD-compatible commands in your IF block, there's no reason to use IF.
 
A serious problem of the if test ( cmnd1 ) else ( cmnd2 ) that iff / else avoids is the many times one of cmnd1 or cmnd2 fails (i.e., an infinte loop, divide by 0, attempt to access an inaccessible device or URL, etc.) if it is evaluated when the test indicates it shuld not be. Using IFF instead eliminates that problem. BTW, IFF was available in 4DOS and 4NT before "command groups" in CMD, so JPsoft customers effectively had command groups with a simple syntax, CMD users had to use GOTO...
 
Thanks Rex, that does clear things up a bit. I knew it had something to do with DO-ENDDO but not quite what and how.

I suggest that we change the following text from the docs (first line after usage)

"IF is most often used only aliases and batch files"

... which I don't understand (but that's probably me), into something like

Do not mix IFF-THEN and DO-ENDDO with if () and do ()

because that seems to be the solution here.

Cheers, DJ
 
In the 4DOS and the 4NT v5 help the quoted sentence read:
IF is normally used only in aliases and batch files.
In the V6 help it was changed to what you quoted:
IF is most often used only aliases and batch files.
The old version intended to mean that one uses IF interactively at the command prompt very rarely, but is common in aliases and - of course - batch files.
As far as I can tell the newer version tried to replace "normally" with "most often", but accidentally deleted the word "in". The change in $nt6/tcmd6 also added itje ELSE clause, and in its description added: Note: this syntax is much less powerful than the IFF command, which is recommended. The description has not changed since.
As I understand it, Any warning for IF...ELSE should be that the parentheses may enclose only a single command, not a command group, which they resemble only visually, not functionally.
 
Thank you Steve, for putting the dots on the i's in my quote.

However, your proposed wording of a warning is not correct. I happen to like the terseness of the if() statement, so I use it frequently. Multi-line and deeply nested and never a problem. I am worried that I unknowingly might have created a vulnerability or performance issue. I don't know anymore. I never had any problems until I stumbledon this example which at that moment struck me as ... illogical - no pun intended?

Since then I learned that if() and iff have a very different history and Rex tells us that they are executed in a different manner. That difference, you pointed out can even "lead to serious problems" in one form. So, my coding no longer being the focus of this discussion, I would like to ask the powers that be to provide us users with (1) a clear(er) guideline when to use which idiom and (2) a more complete description of the consequences we suffer when we do not heed their advice.

In any case, a single note containing "if() has less powerful syntax" and "iff is recommended" does not impress as sufficient user guidance given the sheer size of the documentation. And *that* is my only point: there is room for improvement in this part of the docs.

DJ
 
Back
Top
[FOX] Ultimate Translator
Translate