Computing.Net > Forums > Programming > If Else If ... for Batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

If Else If ... for Batch

Reply to Message Icon

Name: mixty
Date: October 30, 2009 at 20:49:39 Pacific
OS: Windows 7
Subcategory: Batch
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: October 30, 2009 at 21:01:21 Pacific
Reply:

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


0

Response Number 2
Name: mixty
Date: October 30, 2009 at 21:48:56 Pacific
Reply:

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?


0

Response Number 3
Name: Razor2.3
Date: October 30, 2009 at 21:54:51 Pacific
Reply:

Those examples are parsed identically.


0

Response Number 4
Name: Judago
Date: October 31, 2009 at 06:52:33 Pacific
Reply:

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

Response Number 5
Name: Razor2.3
Date: October 31, 2009 at 21:01:26 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Judago
Date: October 31, 2009 at 23:39:56 Pacific
Reply:

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


0

Response Number 7
Name: Mechanix2Go
Date: November 1, 2009 at 01:21:13 Pacific
Reply:

"scripting has so much spaghetti code."

ROTFLMAO


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: If Else If ... for Batch

IF ELSE Error in Batch file www.computing.net/answers/programming/if-else-error-in-batch-file/15450.html

Help for batch script www.computing.net/answers/programming/help-for-batch-script/9732.html

If else statement in Batch file www.computing.net/answers/programming/if-else-statement-in-batch-file/16954.html