if 语句基础

在批处理程序中 if 语句非常有用,本章将仔细根据 if /? 帮助信息来解读 if 语句的基础用法,以及更高级的用法。

if 语句

if 语句执行批处理程序中的条件处理。语法:

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

参数说明:

  • NOT   将表达式的结果取反。例如:表达式为true,not后就为false。只有条件为 false 的情况下,Windows 才应该执行该命令。

  • ERRORLEVEL number   如果最后运行的程序返回一个等于或大于指定数字的退出代码,指定条件为 true。

  • string1==string2   如果指定的字符串 string1 等于 string2,条件表达式为 true,执行 if 语句的命令。

  • EXIST filename   如果指定的文件名存在,指定条件为 true。

  • command   如果符合条件(即表达式为TRUE),指定要执行的命令。如果指定的条件为 FALSE,命令后可跟 ELSE 命令,该命令将在 ELSE 关键字之后执行该命令。

实例:下面通过实例演示上面几种 IF 语句形式的用法。

@echo off
rem 需要用户输入密码
set /p pwd=请输入密码:
if "%pwd%"=="123456" echo 密码正确
if not "%pwd%"=="123456" echo 错误的密码: %pwd%

set file=D:\tmp.txt
if exist %file% echo %file% 文件存在
if not exist %file% echo %file% 文件不存在

rem 下面有意将 copy 的目标文件参数不写,导致 copy 执行失败
rem 此时 errorlevel 等于 1,我们使用 “ERRORLEVEL number” 风格来判断拷贝是否成功
rem 用意:测试 “ERRORLEVEL number” %ERRORLEVEL% 的结果大于等于指定值,表达式为true
copy tmp.txt >nul 2>nul
if errorlevel 1 echo errorlevel=%errorlevel% 拷贝失败
if errorlevel 0 echo errorlevel=%errorlevel% 文件拷贝成功

ipconfig > nul
if errorlevel 0 echo errorlevel=%errorlevel% 执行成功

输出结果:

C:\Users\Administrator\Desktop> test.bat
请输入密码:123
错误的密码: 123
D:\tmp.txt 文件不存在
errorlevel=1 拷贝失败
errorlevel=1 文件拷贝成功
errorlevel=0 执行成功

if-else 语句

上面简单的介绍了 “if” 语句的基础用法。本节我们将介绍 if-else 语句,该语句用来处理 “如果条件为真,执行语句1;否则,执行语句2”。伪语句:

if 条件 == true
    语句1
else
    语句2

根据上面伪语句描述,我们可以使用 IF 加 NOT 实现该功能。

实例:用户输入密码,判断密码是否正确。

@echo off
set /p pwd=请输入密码:
if "%pwd%"=="123456" echo 密码正确
if not "%pwd%"=="123456" echo 错误的密码: %pwd%

输出结果:

C:\Users\Administrator\Desktop> test.bat
请输入密码:123456
密码正确

C:\Users\Administrator\Desktop>test.bat
请输入密码:123455
错误的密码: 123455

上述实例还可以采用 goto 去实现,因为还未介绍 goto 语法,这里就不使用 goto 实现。虽然使用 if + not 能实现该功能,但是代码看上去不是很优雅。我们可以使用批处理提供的 if-else 语句实现。

ELSE 子句必须出现在同一行上的 IF 之后。语法如下:

IF EXIST filename. (
    del filename.
) ELSE (
    echo filename. missing.
)

由于 del 命令需要用新的一行终止,因此以下子句不会有效:

IF EXIST filename. del filename. ELSE echo filename. missing

由于 ELSE 命令必须与 IF 命令的尾端在同一行上,以下子句也不会有效:

IF EXIST filename. del filename.
ELSE echo filename. missing

如果都放在同一行上,以下子句有效(将 del 语句放入括号中):

IF EXIST filename. (del filename.) ELSE echo filename. missing

实例:用户输入密码,判断密码是否正确。

@echo off
set /p pwd=请输入密码:
if "%pwd%"=="123456" (
    echo 密码正确
) else (
    echo 错误的密码: %pwd%
)

输出结果:

C:\Users\Administrator\Desktop> test.bat
请输入密码:123456
密码正确

C:\Users\Administrator\Desktop>test.bat
请输入密码:123455
错误的密码: 123455

if-elseif-else 语句

如果条件分支比较少,使用 if-else 就可以了。但是,如果你遇到条件非常多的怎么处理呢?if-else 也能处理,批处理提供了 if -elseif-else 类似 switch 语句。实例:根据学生输入的成绩给出学生级别。代码如下:

@echo off
set /p score=输入成绩:
if %score% lss 60 (
    echo 未及格,需要多努力啊!
) else if %score% lss 70 (
    echo 及格,多做题
) else if %score% lss 80 (
    echo 良,加把劲考
) else if %score% lss 90 (
    echo 优,你很优秀,继续保持
) else (
    echo 不是凡人
)
pause

输出结果:

C:\Users\Administrator\Desktop>test.bat
输入成绩:78
良,加把劲考        
请按任意键继续. . .

如果你要将 else if 换行,需要在 “)” 后面添加 “^” 符号。如下:

@echo off
set /p score=输入成绩:
if %score% lss 60 (
    echo 未及格,需要多努力啊!
) ^
else if %score% lss 70 (
    echo 及格,多做题
) ^
else if %score% lss 80 (
    echo 良,加把劲考
) ^
else if %score% lss 90 (
    echo 优,你很优秀,继续保持
) ^
else (
    echo 不是凡人
)
pause

输出结果:

C:\Users\Administrator\Desktop>test.bat
输入成绩:78
良,加把劲考        
请按任意键继续. . .
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号