Articles

Solved If Else If ... for Batch

October 30, 2009 at 20:49:39
Specs: Windows 7

Hi there!

I would like to post a question here about
batch file and "if else if" support.
I have googled and found people suggest
using "nested if" to achieve the same effect in
a batch.

But does batch file really not support "if else
if"?
I've tested in XP and Win7, a line like the one
below does work though. (It outputs d, since
all three previous are wrong)

@echo off
if 1==2 (echo a) else if 1==3 (echo b) else if
1==4 (echo c) else (echo d)

And I've seen the Windows PE built by
Symantec for Ghost 11.5 uses this type of if
else if as well:

if exist c:\ghost\unattend.xml (
wpeinit /unattend=c:\ghost\unattend.xml
) else if exist x:\ghost\unattend.xml (
wpeinit /unattend=x:\ghost\unattend.xml
) else (
wpeinit
)

But still why "nested if" is recommended over
this? For compatibility with Win9x? (Just
wondering). Or am I fundamentally wrong?
Thanks!


See More: If Else If ... for Batch

Report •


✔ Best Answer
October 31, 2009 at 06:52:33

As razor points out, both examples are functionally identical.

I think what you may be missing is the idea of "nesting" itself, basically all it means in this context is an "if statement being executed by another if statement".

Formatting is irrelevant, it's all about the logic of the statements. Nesting if statements, with or with out else clauses, is used to ensure multiple conditions are met.

That said formatting plays an important role in readability and each format has it's strengths.

In my opinion the single line if-else gets confusing when nested, using full code blocked style is good for nest if without else and the inline if-on-else is good for nested if else:

if 1==1 (do command) else do command

if 1==1 (
    if 2==2 (
        if 3==3 (
            do command
        )
    )
)

if 1==1 (
    do command
) else if 2==2 (
    do command
) else if 3==3 (
    do command
)

Of course it all depends on how you are using it, a big factor is what you have to execute, multiple commands sit well in code block while with a single command they can be unnecessary. The styles above are just preference, to each his own.


As for the mention of compatibility with 9x batch script, I very much doubt it. On those systems there is no else clause or code blocks and the statement must be contained in one line. The only way 9x if statements can be nested is like so:

if 1==1 if 2==2 if 3==3 echo Yay!

Line length limits catch up pretty quickly on those systems though....


Batch Variable how to



#1
October 30, 2009 at 21:01:21

I believe they're saying those examples are nested. Each IF statement is nested in the previous' ELSE statement.

Report •

#2
October 30, 2009 at 21:48:56

Thanks for the reply!

The example I gave was a "if else if" one (as I called it):
if 1==2 (echo a) else if 1==3 (echo b) else if
1==4 (echo c) else (echo d)

if it was represented in "nested if", it would have been
if 1==2 (
echo a
) else (
@if 1==3 (
echo b
) else (
@if 1==4 (
echo c
) else (
echo d
)
)
)

Both works the same (output is d). I wonder why not to use "if
else if" instead of "nested if?


Report •

#3
October 30, 2009 at 21:54:51

Those examples are parsed identically.

Report •

Related Solutions

#4
October 31, 2009 at 06:52:33
✔ Best Answer

As razor points out, both examples are functionally identical.

I think what you may be missing is the idea of "nesting" itself, basically all it means in this context is an "if statement being executed by another if statement".

Formatting is irrelevant, it's all about the logic of the statements. Nesting if statements, with or with out else clauses, is used to ensure multiple conditions are met.

That said formatting plays an important role in readability and each format has it's strengths.

In my opinion the single line if-else gets confusing when nested, using full code blocked style is good for nest if without else and the inline if-on-else is good for nested if else:

if 1==1 (do command) else do command

if 1==1 (
    if 2==2 (
        if 3==3 (
            do command
        )
    )
)

if 1==1 (
    do command
) else if 2==2 (
    do command
) else if 3==3 (
    do command
)

Of course it all depends on how you are using it, a big factor is what you have to execute, multiple commands sit well in code block while with a single command they can be unnecessary. The styles above are just preference, to each his own.


As for the mention of compatibility with 9x batch script, I very much doubt it. On those systems there is no else clause or code blocks and the statement must be contained in one line. The only way 9x if statements can be nested is like so:

if 1==1 if 2==2 if 3==3 echo Yay!

Line length limits catch up pretty quickly on those systems though....


Batch Variable how to


Report •

#5
October 31, 2009 at 21:01:26

Line length limits catch up pretty quickly on those systems though....
I believe the limit's 256 characters. It's really only enough for two IF statements, or a FOR IF pairing. There's a reason why traditional batch scripting has so much spaghetti code.

Report •

#6
October 31, 2009 at 23:39:56

Ahh, nothing like hearing that beep or making something pretty with ansi.sys only to find out the escape sequences push it over......


Batch Variable how to


Report •

#7
November 1, 2009 at 01:21:13

"scripting has so much spaghetti code."

ROTFLMAO


=====================================
Helping others achieve escape felicity

M2


Report •


Ask Question