windows - Why won't portions of the code from this batch file execute. Please explain the GOTO function -
i playing around bit batch files , having problems. according 1 website:
to exit batch script file or exit subroutine specify goto:eof transfer control end of current batch file or end of current subroutine.
so, definition in mind, why won't portions of these 2 codes execute:
first one:
:loop echo in loop. goto:eof echo hello goto loop echo finish pause
the thing prints is: in loop. nothing else prints.
second one:
echo hello goto loop echo finish pause :loop echo in loop goto:eof
echo finish not print. why?
also, can briefly state what's difference between goto use , subroutines?
thanks
update: while searching on google else, found this: using call labels in batch script guess answers question, i still elaboration please, such as
1) when use goto , when use call? (which suppose related question above differences between subroutines , goto)
2) first code, why sentence: "i in loop." print / echoed, when never called upon / instructed executed?
2b) how can portion executed when it's called upon?
update 2:
well, how can work?
@echo off echo main line call :loop call :another call :loop echo main line again goto :eof :loop echo inside subroutine :another echo hi!
the mission of goto
command change next line execute jumping existing label. first line after label next line execute. independly of how many goto
s used in code, if execution reaches end of file (and include goto :eof
) or exit /b
reached, execution of batch file ends.
to use subroutine, necessary use call :label
command. when command executed, second line of execution created. first line (the calling code) suspended until second line (the subroutine) ends, , execution on first line continue @ first command after call
.
when subrotine end execution?
in other languages subrotines have starting point , ending point, kind of syntax in declaration delimits code belongs subroutine.
this not case in batch files. subroutines have starting point, label called, , execution not end when label reached.
the subrotines end when execution line ends , happens when exit /b
executed or end of batch file reached. in both cases execution line of subroutine ends , execution line of calling code continues.
and, of course, in scenario, if exit
command (without /b
switch) executed, current batch , current console closed.
for questions
1) when use goto
, when use call
? depends. should used when needed , determined problem solve, ideas on how solve , way code.
2) labels in batch code not define functions nor bound anything. defined points in code possible jump or call. no interfere in code execution, so, parsed, discarded if not needed in moment (more or less), , process continue on next line. labels not barriers protect/block code after them. in code, initial label stepped over, , first executable line echo
command. after it, goto:eof
ends execution of batch file.
2b) if don't want execute line unless invoked, ensure there no way can reached unless called/jumped to.
@echo off echo main line call :loop echo main line again goto :eof :loop echo inside subroutine
Comments
Post a Comment