|
pla920
新手上路

积分 3
发帖 3
注册 2009-11-8
状态 离线
|
『楼 主』:
求助批处理中if与goto的关系
使用 LLM 解释/回答一下
本人菜鸟,看了一下段教程是关于if语句的,其中提到一条if语句中有多条命令和多个变量的时候,则命令和变量需要goto语句引导!否则if语句只会调用第一条命令和变量.
-------------------------------------------------其中范例如下:
@echo on
set /p num=请输入要自信的操作:
if "%num%"=="1" (
set /p a=请输入内容:
set /p b=请输入文件名:
set /p c=请输入复制后的文件名:
goto a
:a
echo %a% >%b%.txt
goto b
:b
copy %b%.txt c:\%c%.txt
)
pause
set a=
set b=
set c=
set num=
-----------------------------------------------------------------
只想请问一下高手这个goto是怎么样调用的,不是说批处理命令是从上至下一次执行的吗?如果是这样这两条goto语句的意义又何在呢?只没有goto语句的情况下,所生成的txt文件是没有没有题目的,以.txt存在.就是上面所说的情况.本人编程不是很懂,希望有知情者能予以告知,在此先谢谢了:)
I'm a novice. I read a tutorial about the if statement, which mentions that when there are multiple commands and multiple variables in an if statement, the commands and variables need to be guided by the goto statement! Otherwise, the if statement will only execute the first command and variable.
-------------------------------------------------The example is as follows:
@echo on
set /p num=Please enter the operation you want to be confident about:
if "%num%"=="1" (
set /p a=Please enter content:
set /p b=Please enter file name:
set /p c=Please enter the file name after copying:
goto a
:a
echo %a% >%b%.txt
goto b
:b
copy %b%.txt c:\%c%.txt
)
pause
set a=
set b=
set c=
set num=
-----------------------------------------------------------------
I just want to ask the master how this goto is called. Isn't it said that batch processing commands are executed one by one from top to bottom? If so, what is the meaning of these two goto statements? Without the goto statement, the generated txt file has no title and exists as .txt. That's the situation mentioned above. I don't understand programming very much. I hope those who know can inform me. Thank you here first:)
|
|
2009-12-6 23:19 |
|
|
slore
铂金会员
      
积分 5212
发帖 2478
注册 2007-2-8
状态 离线
|
『第 2 楼』:
请体会引导的含义
使用 LLM 解释/回答一下
@echo off
set num=1
if "%num%"=="1" (
set /p a1=请输入a1:
set /p b1=请输入b1:
goto label1
:label1
echo %a1%
echo %b1%
)
pause
if "%num%"=="1" (
set /p a2=请输入a2:
set /p b2=请输入b2:
echo %a2%
echo %b2%
)
pause
rem 启用延时变量
setlocal EnableDelayedExpansion
if "%num%"=="1" (
set /p a3=请输入a3:
set /p b3=请输入b3:
echo !a3!
echo !b3!
)
pause
则命令和变量需要goto语句引导!
这个goto命令不是要要实现逻辑上跳转的,逻辑上不要他依然要执行
到echo
这个是cmd处理变量的机制问题。
goto或者开启变量延时才能得到if/for () 块内部的变量
```batch
@echo off
set num=1
if "%num%"=="1" (
set /p a1=请输入a1:
set /p b1=请输入b1:
goto label1
:label1
echo %a1%
echo %b1%
)
pause
if "%num%"=="1" (
set /p a2=请输入a2:
set /p b2=请输入b2:
echo %a2%
echo %b2%
)
pause
rem 启用延时变量
setlocal EnableDelayedExpansion
if "%num%"=="1" (
set /p a3=请输入a3:
set /p b3=请输入b3:
echo !a3!
echo !b3!
)
pause
Then the commands and variables need the goto statement to be guided!
This goto command is not to achieve logical jump, logically it still has to execute to echo
This is a mechanism problem of cmd processing variables.
Only by goto or enabling variable delay can we get the variables inside the if/for () block
```
|

S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore |
|
2009-12-7 11:51 |
|
|
pla920
新手上路

积分 3
发帖 3
注册 2009-11-8
状态 离线
|
『第 3 楼』:
谢谢2楼,这是我的理解不知道对不对,请各位审阅.
使用 LLM 解释/回答一下
2楼的意思是不是说,goto在if /for()语句中只是起到引导变量的作用,没有逻辑跳跃作用.
想在问几个问题是不是这种情况,只是在if和for语句中发生?如果有,是不是都能通过goto语句引导来解决这些这样类似的问题,还有另有其他的方法(如二楼给出的加入setlocal EnableDelayedExpansion和双!的方法)?谢谢(还菜鸟就是无知请多多见谅.)
Does the meaning of the second floor mean that goto in the if/for() statement only plays the role of guiding variables, and there is no logical jump role.
I want to ask a few more questions. Is this the case, only occurring in if and for statements? If so, can all be solved by guiding with goto statements to solve such similar problems, and are there other methods (such as the method of adding setlocal EnableDelayedExpansion and double! given by the second floor)? Thank you (still a novice, just ignorant, please forgive me a lot.)
|
|
2009-12-7 20:03 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
可以这样理解这个问题。在if 或for的内部,变量动态的捕获一般需要变量延迟。
至于使用goto还是setlocal EnableDelayedExpansion,原则上都可以,但例如如果需要动态捕获10个变量,这个时候需要10个goto,会增加代码的使用量,就不如一个setlocal EnableDelayedExpansion方便,也更让读代码的人一目了然。
在实际上用中,不推荐在if或for的内部使用goto 。
This can be understood in this way. Inside if or for, dynamic capture of variables generally requires variable delay. As for using goto or setlocal EnableDelayedExpansion, in principle, both are acceptable, but for example, if 10 variables need to be captured dynamically, then 10 gotos are needed at this time, which will increase the amount of code used. It is not as convenient as a setlocal EnableDelayedExpansion, and it is also more clear to the person reading the code. In actual use, it is not recommended to use goto inside if or for.
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2009-12-10 15:25 |
|
|