If...Else If - how to make this work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

If...Else If - how to make this work

#1 Post by Jer » 27 Jun 2016 18:13

This block of code is being defiant. It is illustrative of something I am
trying to do in a larger batch program. None of the paths are taken when i run it:

Code: Select all

@Echo Off
setlocal EnableDelayedExpansion
set "var1A=8A" & Set "var1B=8A"
Set "var2A=179" & Set "var2B=197"
echo vars:
set var
If not "!var1A!"=="!var1B!" If not "!var2A!"=="!var2B!" (
   echo vars not duplicated within sets
) Else If "!var1A"=="!var1B!" (
   echo var1A and var1B are duplicated
) Else If "!var2A!"=="!var2B!" (
   echo var2A and var2B are duplicated
) Else (
   echo took the ...else path
)

endlocal & exit /b

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: If...Else If - how to make this work

#2 Post by aGerman » 27 Jun 2016 18:50

An exclamation mark is missing in the first Else If statement. But that's not the reason why it doesn't work. The result of the first If statement is already FALSE. That's why not even the second If statement was executed. There are several posibilities to work around. E.g. viewtopic.php?t=4308
I prefer an additional variable that was initially set to 0. Using += or |= operator with powers of 2 you will get a unique result.

Code: Select all

set /a "n=0"
If "!var1A!"=="!var1B!" set /a "n|=1"
If "!var2A!"=="!var2B!" set /a "n|=2"
If !n! equ 0 (
   echo vars not duplicated within sets
) Else If !n! equ 1 (
   echo var1A and var1B are duplicated
) Else If !n! equ 2 (
   echo var2A and var2B are duplicated
) Else (
   echo took the ...else path
)

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: If...Else If - how to make this work

#3 Post by Jer » 27 Jun 2016 20:31

aGerman, as you pointed out, my test code was missing a ! mark.

Thanks and I will use your suggestion of an additional variable to set up a value to be tested in an if-block
that will perform more than one test.

Lesson learned, and correct me if I'm wrong: do not start a if-else if-else block with a compound test (testing for
more than 1 condition on a single line) unless you want to use the opening statement FALSE condition to skip the entire block.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: If...Else If - how to make this work

#4 Post by siberia-man » 05 Jul 2016 07:22

Jer

You can try this one: Conditionals on steroids

It allows to implement something like this one

Code: Select all

If cond1 And cond2 Then
Else If cond3 Or cond4 Then
Else
End If


in the form as here

Code: Select all

call :if cond1 && call :if cond2 && (
   rem do something1
) || call :if cond3 || call :if cond4 && (
   rem do something2
) || (
   rem do something3
)

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: If...Else If - how to make this work

#5 Post by trebor68 » 11 Jul 2016 01:52

Nesting of IF-blocks is possible, but each of IF blocks must be set up correctly.

Code: Select all

IF cond (TRUE block) else (FALSE block)

Or you can write this in this form:

Code: Select all

IF cond (
  TRUE block
) else (
  FALSE block
)


Now inserting a second condition:

Code: Select all

IF cond1 (
  if cond2 (
    RESULT1 block
  ) else (
    RESULT2 block
  )
) else (
  if cond2 (
    RESULT3 block
  ) else (
    RESULT4 block
  )
)

When is using wich RESULT block:
RESULT1: cond1 is TRUE and cond2 is TRUE
RESULT2: cond1 is TRUE and cond2 is FALSE
RESULT3: cond1 is FALSE and cond2 is TRUE
RESULT4: cond1 is FALSE and cond2 is FALSE

Now this construct with a part of your code:

Code: Select all

If not "!var1A!"=="!var1B!" (
   If not "!var2A!"=="!var2B!" (
      echo vars not duplicated within sets
   ) else (
      echo var2A and var2B are duplicated
   )
) else (
   If not "!var2A!"=="!var2B!" (
      echo var1A and var1B are duplicated
   ) else (
      echo took the ...else path
   )
)

It is therefore possible. But a better solution is that of aGerman, as you the code can easily extend to several conditions.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: If...Else If - how to make this work

#6 Post by Aacini » 11 Jul 2016 07:54

Code: Select all

if "!var1A!+!var2A!" equ "!var1B!+!var2B!" (
   echo took the ...else path
) Else If "!var1A!" == "!var1B!" (
   echo var1A and var1B are duplicated
) Else If "!var2A!" == "!var2B!" (
   echo var2A and var2B are duplicated
) Else (
   echo vars not duplicated within sets
)

Antonio

Post Reply