Any internal and external commands have this very simple syntax:
{command}, then optional [parameters...]
In IF command, the "IF" is the command, and *any* text following it, is its
parameters. Thus, the "%%a geq 3 if %%a leq 5..." is the whole parameters of
the first IF command.
In the first IF context, its condition parameter is "%%a geq 3", and its
conditional command parameter is the second IF, which is "if %%a leq 5
(echo...". All text following the second IF, which is "%%a leq 5 (echo...",
is the whole parameters for the second IF. i.e. the conditional command's
parameters of the first IF.
So, your code is actually interpreted like this:
if %%a geq 3 (
if %%a leq 5 (
echo number is 3, 4 or 5
) else (
echo number is less than 3 or greater than 5
)
)
To achieve what you wanted, you'll need to provide commands for both true
and false result of the IF condition. i.e.
if %%a geq 3 (
if %%a leq 5 (
echo number is 3, 4 or 5
) else (
echo number is less than 3 or greater than 5
)
) else (
echo number is less than 3 or greater than 5
)
To understand complex IF commands or grouped commands, format the code in a
hierarchical manner like above, and insert an opening command group each
time a command parameter is a subcommand. e.g.
if %%A==1 for /l %%C in (1,1,3) do if %%C==2 if %%B==3 echo abc&echo xyz
Is actually...
if %%A==1 (
for /l %%C in (1,1,3) do (
if %%C==2 (
if %%B==3 (
echo abc
echo xyz
)
)
)
)
To avoid confusion when building multiple IF commands, always use command
grouping on all conditional commands of each IF command. Only after you've
ensured that it has worked the way you wanted, you can safely remove any
unnecessary command grouping. This also applies to other command which
requires subcommand such as FOR. e.g.
if %%A==0 ( if %%B==0 ( echo abc ) )
for %%A in (a b c) do ( echo %%A )