Page 1 of 1
If...Else If - how to make this work
Posted: 27 Jun 2016 18:13
by Jer
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
Re: If...Else If - how to make this work
Posted: 27 Jun 2016 18:50
by aGerman
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=4308I 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
)
Re: If...Else If - how to make this work
Posted: 27 Jun 2016 20:31
by Jer
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.
Re: If...Else If - how to make this work
Posted: 05 Jul 2016 07:22
by siberia-man
JerYou can try this one:
Conditionals on steroidsIt 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
)
Re: If...Else If - how to make this work
Posted: 11 Jul 2016 01:52
by trebor68
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 TRUERESULT2:
cond1 is TRUE and
cond2 is FALSERESULT3:
cond1 is FALSE and
cond2 is TRUERESULT4:
cond1 is FALSE and
cond2 is FALSENow 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.
Re: If...Else If - how to make this work
Posted: 11 Jul 2016 07:54
by Aacini
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