Odd IF behavior

This seems odd to me and I wanted to find out why. When I run this, the set line does not fire if the if statement above it is false.

If I use iff/endiff instead it works, or if I remove the FOR statement it works, but I would think that it should work as written.

Code:
for /L %i in (1,1,1) do (
        if defined DEBUG then echo THIS IS A TEST
        set ___DEBUGTEST=test
)
What am I missing?

Michael
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,425
85
Albuquerque, NM
prospero.unm.edu
This seems odd to me and I wanted to find out why. When I run this, the set line does not fire if the if statement above it is false.

If I use iff/endiff instead it works, or if I remove the FOR statement it works, but I would think that it should work as written.

Code:
for /L %i in (1,1,1) do (
        if defined DEBUG then echo THIS IS A TEST
        set ___DEBUGTEST=test
)
What am I missing?

Michael

FOR is inherently a single-line command, so all of your lines above are contatenated into one. The setting "Duplicate CMD.EXE bugs" (near the bottom of the right column on the first page of the OPTION dialog) applies....

(Also note that the THEN keyword only works with IFF, not IF.)
 

samintz

Scott Mintz
May 20, 2008
1,488
15
Solon, OH, USA
A FOR statement is not really a multiline statement. It is a single
statement. So the command for your FOR is effectively

if defined DEBUG then echo THIS IS A TEST & set ___DEBUGTEST=test

You are always better off using a DO loop when you have multiple lines.

-Scott

frossm <> wrote on 01/22/2009 03:40:27 PM:


> This seems odd to me and I wanted to find out why. When I run this,
> the set line does not fire if the if statement above it is false.
>
> If I use iff/endiff instead it works, or if I remove the FOR
> statement it works, but I would think that it should work as written.
>
>
> Code:
> ---------
> for /L %i in (1,1,1) do (
> if defined DEBUG then echo THIS IS A TEST
> set ___DEBUGTEST=test
> )
> ---------
> What am I missing?
>
> Michael
>
>
>
>
 
Thanks Scott and Charles. I will switch the bigger btm over to a do
statement.

It would also make it easier to debug as well!

Much appreciated.

Michael




On Jan 22, 2009, at 3:14 PM, samintz <> wrote:


> A FOR statement is not really a multiline statement. It is a single
> statement. So the command for your FOR is effectively
>
> if defined DEBUG then echo THIS IS A TEST & set ___DEBUGTEST=test
>
> You are always better off using a DO loop when you have multiple
> lines.
>
> -Scott
>
> frossm <> wrote on 01/22/2009 03:40:27 PM:
>
>
>
> ---Quote---
>> This seems odd to me and I wanted to find out why. When I run this,
>> the set line does not fire if the if statement above it is false.
>>
>> If I use iff/endiff instead it works, or if I remove the FOR
>> statement it works, but I would think that it should work as written.
>>
>>
>> Code:
>> ---------
>> for /L %i in (1,1,1) do (
>> if defined DEBUG then echo THIS IS A TEST
>> set ___DEBUGTEST=test
>> )
>> ---------
>> What am I missing?
>>
>> Michael
>>
>>
>>
>>
> ---End Quote---
>
>
>
 
Similar threads
Thread starter Title Forum Replies Date
Charles Dye Odd behavior after using OPTION "Advanced" tab Support 2
C odd behavior of "dir" Support 0
J Odd OPTION behavior Support 1
R Odd Behaviour With NET USE Support 3
G Odd problem - screen brightness Support 4
mfarah WAD Odd behaviour with command-line arguments on TCMD. Support 7
Dan Glynhampton Documentation v15 help: More odd mailto: links in the help Support 8
D odd experience updating tcmd 13 Support 2
H Very odd issue with Help in 12.10.67 Support 3
J Odd V12 TCMD issue Support 5
H @XMLNODES - New behavior? Support 3
Jesse Heines Strange Line Wrapping Behavior Support 14
H Behavior change re piping a string to a Java program Support 1
Jay Sage Baffling Behavior With TPIPE Support 5
D Pasting Unicode data has different behavior on TCC and CMD Support 2
P "Set" command behavior - a bug? Support 3
P LOG command behavior Support 3
I Puzzling Behavior of TDRV and DEL Support 2
N Fixed Strange dir behavior Support 6
A Inconsistent @WINPOS // ACTIVATE /POS= behavior Support 0
P Strange mouse behavior with list Support 2
R Find in screen buffer - find next behavior Support 0
D v20 code page behavior changes Support 2
D Strange DO behavior with /O Support 5
Jay Sage Change in Behavior of Activate Command Support 4
deleyd Fixed Behavior different than CMD Support 1
jwiede Difference in "FOR /F" behavior (?) Support 1
R Difference in behavior of RMDIR vs. CMD.EXE? Support 5
D Fixed Change in behavior of variable expansion between 16 and 17 Support 3
D Unexpected behavior Support 8
D Bash-like Esc->Backspace behavior Support 4
C ZIP behavior Support 5
M How to? Work around an unexpected and very inconvenient behavior of TCC... Support 26
A expected behavior of move /sx Support 14
M WAD A complaint regarding a very irritating TCC installation behavior... (Bug?) Support 4
S Inexplicable behavior difference in nearly identical shortcuts Support 6
Peter Bratton @ping behavior change Support 2
vefatica SHORTCUT behavior (Win7) Support 3
M A question about new behavior that’s a minor irritation… Support 16
M Strange behavior... Support 2
S MSGBOX - inconsistent behavior Support 1
vefatica @REGEX: behavior vs. documentation Support 2
vefatica Strange behavior reloading SHRALIAS sav files. Support 1
S Strange CHKDSK behavior Support 6
M (Somewhat unbelievable) Do While Behavior/Question Support 1
S RECYCLE /P - unexpected behavior Support 0

Similar threads