1

One day, I'm bored, I opened up cmd and typed in this piece of code:
@echo off & color 0a & set /a count=0 & main & set /a count+=1 & echo %count% & goto main
And surprisingly, the program executed for only a second. 0_o

I then wrote the code in the normal batch script format, which is like:

@echo off
color 0a

set /a count=0

:main
set /a count+=1
echo %count%
goto :main

Obviously no errors, program runs perfectly.

So what's going on with the 1st piece of code? The command is the same (different format though) but why is it executed for only a sec?

Thanks in advance!

Happy Face
  • 1,061
  • 7
  • 18
  • Might be wrong, but I think you need `&&` instead of `&`. –  Aug 01 '15 at 09:08
  • 2
    no, `&` is fine and correct. This is a [delayed expansion problem](http://stackoverflow.com/a/30284028/2152082) (and of course, `goto` doesn't work on commandline - even if the label was correct (`:main`)) – Stephan Aug 01 '15 at 09:16

1 Answers1

2

Your main doesn't have a colon which a label requires, and you can't have a label inside a compound statement using a goto - which is your essential problem.

foxidrive
  • 40,353
  • 10
  • 53
  • 68