Board logo

标题: 如何在if语句的条件里实现逻辑或? [打印本页]

作者: xintian     时间: 2007-6-27 15:31    标题: 如何在if语句的条件里实现逻辑或?

请问如何实现 if ("str1"=="str2" or "str3"=="str4") (command)的功能
我正在写一个批处理,如果能实现上面的用法,代码会简单很多,并且易读性更好
如果是and ,可以简单地用嵌套,换成or好像不大好实现
另外,大家也可考虑下,如果是xor呢
作者: xintian     时间: 2007-6-27 15:43
补充:
我不知道if语句是否支持逻辑运算符,如果支持当然最好,上面的问题自然很好解决。
如果不支持,我目前的办法是用
if "str1"=="str2" (command) else (if "str3"=="str4" (command))
但是这样写的话,如果(command)段代码很多,就会重复很多
不知有没有什么更好的办法?
作者: zasxcdfv     时间: 2007-6-27 15:45
可以的呀。批处理的if语句也可以用else的。或者也可以用||,虽然我个人不太喜欢这玩意……
详见if/?

看了你二楼的内容,我想应该可以用if嵌套实现吧。
作者: xintian     时间: 2007-6-27 15:58
是这样吗?
if ("str1"=="str2" || "str3"=="str4") (command)
但是系统提示 "此时不应有 ||"
作者: xintian     时间: 2007-6-27 16:01
而且if/?里也没有提到||
作者: zasxcdfv     时间: 2007-6-27 16:03
if else 不能解决问题吗?||等待高手讲解……我还迷糊

刚才的程序理解错了~

[ Last edited by zasxcdfv on 2007-6-27 at 04:05 PM ]
作者: xintian     时间: 2007-6-27 16:04
还是提示 "此时不应有 ||。"

[ Last edited by xintian on 2007-6-27 at 04:06 PM ]
作者: caruko     时间: 2011-1-24 17:07
可以用 set /a 的逻辑或来计算
比如
if "str1"=="str2" set /a m=1
if "str3"=="str4" set /a n=1
set /a x=m "|" n
if %x% EQU 1 command
作者: Hanyeguxing     时间: 2011-1-24 19:18
if "str1"=="str2" set m==
if "str3"=="str4" set m==
if defined m command