Welcome!

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

SignUp Now!

WAD Nested if exits enclosing if

Apr
2
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 ( )
)
 
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.

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.
 
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.

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.
 
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.
 

Similar threads

Back
Top