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

WAD Nested if exits enclosing if

Discussion in 'Support' started by Greg, Aug 27, 2012.

  1. Greg

    Joined:
    Apr 6, 2009
    Messages:
    2
    Likes Received:
    0
    I'm seeing this on TCC 14.01.33 x64 but it was the same in previous versions, so maybe this isn't a bug? If a nested IF statement has a false condition and does not have an ELSE block, then the enclosing IF block is exited.

    @echo off
    set outer=1
    set inner1=
    set inner2=1

    rem Possible bug: Should print "inner2" but does not
    if defined outer (
    echo outer

    if defined inner1 (
    echo inner1
    )

    if defined inner2 (
    echo inner2
    )
    )

    rem Workaround: add empty else block
    if defined outer (
    echo outer

    if defined inner1 (
    echo inner1
    ) else ( )

    if defined inner2 (
    echo inner2
    ) else ( )
    )
     
  2. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    10,029
    Likes Received:
    84
    Not a bug; it's a CMD-compatibility issue.

    Go to "OPTION / Startup", and uncheck "Duplicate CMD.EXE bugs", and it will behave the way you want. See the help for IF for more details.
     
  3. Greg

    Joined:
    Apr 6, 2009
    Messages:
    2
    Likes Received:
    0
    Thanks. BTW, this works now under CMD using Win 7 Version 6.1.7601 and XP Version 5.1.2600 (SP3). But, they don't like the empty else block.
     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,355
    Likes Received:
    39
    I recommend using the IFF block instead wherever possible. It's neater, inherently multiline, and not hogtied by compatibility with the (shifting?) CMD.EXE target.
     

Share This Page