That is a confusing example not just because the output, but because the way that it is generated:
Code: Select all
@echo off
if A equ A (
GOTO :EXAMPLE_LABEL
:EXAMPLE_LABEL
rem
) else (
echo You didn't expected to see this,did you?
)
The GOTO command break the IF and start to search for the label from the
next parsed block of code, that is the code placed
after the IF! The label is not found and it is searched again from the beginning of the file. When the label is reached, the execution continue with no code block pending, so the ") else (" is just ignored and the next ECHO executed. I like this!!!
I posted a similar convoluted example
in SO, but it is related to duplicate labels:
Code: Select all
@echo off
set var=1
if %var% equ 1 (
:NEXT
if %var% neq 1 goto NEXT
echo This appear first time: var=%var%
set var=2
goto NEXT
:NEXT
echo This appear second time: var=%var%
)
Antonio