問題描述
批處理文件“and if”語句 (Batch‑file "and if" statement)
我完全不知道如何為我的批處理文件製作“and if”語句,我希望我能在這裡得到一些幫助。
我正在嘗試製作的行:
if %a%==X/O and if %b%==X/O goto done
參考解法
方法 1:
In batch, if statements are quite simple, but unfortunately don't have the same amount of functionality of many other coding languages such as JavaScript.
In batch, to write an if statement, there are only 4 parts. Of course at the beginning you have to specify that it is a if statement, then you add the two things you will be comparing, and the operator. Once that is done, you specify the function you want the program to do if the condition is true. For example:
if "%type%" == "5" goto end
Also, one last thing to note. If your condition is false, the batch file will simply go to the next line. So you might want to add something like this:
:start
set num1=1
set num2=2
set num3=3
set /a totalnum=%num1%+%num2%+%num3%
if "%totalnum%" == "100" goto end
goto start
:end
exit
方法 2:
Batch doesn't have support for and
s or or
s in if
logic. You can simulate an and
by nesting your conditions.
if %a%==X/O (
if %b%==X/O (
goto done
)
)
Similarly, you can simulate an or
by having two separate checks.
if %a%==X/O goto done
if %b%==X/O goto done
(by conecall、Jacob Ward、SomethingDark)