Odd IF behavior

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
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
3,385
39
Albuquerque, NM
prospero.unm.edu
#2
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,204
11
Solon, OH, USA
#3
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
>
>
>
>
 
#4
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---
>
>
>