N
N
narem2019-10-07 10:45:24
cmd/bat
narem, 2019-10-07 10:45:24

Why does the condition not work and does not display the result?

@echo off
set /p option =
if '%option%' == '1' (goto ":math")
if '%option%' == '2' (start "test.txt")

:math
echo 2+2
pause

1 condition always works, even when I enter 2.
It is also unclear how to make it count 2 + 2 and not just output

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2019-10-07
@res2001

1.Arithmetic operations are only supported in the command set /a(see set /?)
2.The quotes in the condition must be double.
3. Post the output option ( ) after set /p to make sure that the variable contains the value that you entered from the keyboard. 4. Labels in goto are specified without quotes 5. As far as I can tell, you need to do exit /b between the second if and the label. Now the code in the label will work for you anyway, even if you enter 2 (in this case, the test.txt file will open and 2 + 2 will be displayed). PS: now I don't have Windows at hand, I can't test your codeecho %option%

@echo off
set /p "option="
if "%option%" == "1" (goto:math)
if "%option%" == "2" (start "test.txt")
exit /b

:math
set /a "val=2+2"
echo %val%
pause

A
AUser0, 2019-10-07
@AUser0

@echo off

set /p option=Enter choice:
if '%option%' == '1' (goto math)
if '%option%' == '2' (
start "test.txt"
goto end
)

:math
set /a sum=2+2
echo %sum%
pause

:end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question