1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unexpected behavior

Discussion in 'Support' started by djspits, Dec 13, 2013.

  1. djspits

    Joined:
    Apr 13, 2010
    Messages:
    189
    Likes Received:
    2
    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
     
  2. pizzaman

    Joined:
    Oct 24, 2011
    Messages:
    53
    Likes Received:
    0
    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
     
    #2 pizzaman, Dec 14, 2013
    Last edited: Dec 14, 2013
  3. djspits

    Joined:
    Apr 13, 2010
    Messages:
    189
    Likes Received:
    2
    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
     
  4. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,809
    Likes Received:
    82
    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.
     
  5. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    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...
     
  6. djspits

    Joined:
    Apr 13, 2010
    Messages:
    189
    Likes Received:
    2
    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
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    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.
     
  8. djspits

    Joined:
    Apr 13, 2010
    Messages:
    189
    Likes Received:
    2
    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
     
  9. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,809
    Likes Received:
    82
    There is no reason to ever use IF (and many reasons not to) when you're going to execute more than one command following the IF.
     

Share This Page