[英]How to use “And” and “Or” keywords together in a same “If” statement
I'm trying to use And
and Or
keywords on a single If statement in my VB Script, which I use with VLC Media Player to stream using command line. 我试图在VB脚本中的单个If语句上使用
And
和Or
关键字,该语句与VLC Media Player一起使用以通过命令行流式传输。 I tried it like: 我像这样尝试:
If (CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr)) And (((CInt(TSSEPStr) - CInt(PTSSEPStr)) <= 5) Or ((CInt(TSSEPStr) - CInt(PTSSEPStr)) >= -5)) Then
I tried putting parentheses for the whole If
statement, but it did nothing. 我尝试为整个
If
语句加上括号,但没有执行任何操作。
If ((CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr)) And (((CInt(TSSEPStr) - CInt(PTSSEPStr)) <= 5) Or ((CInt(TSSEPStr) - CInt(PTSSEPStr)) >= -5))) Then
When I execute my script, only below two conditions seems to work. 当我执行脚本时,似乎只有以下两种情况有效。
1.If ((CurrentEP >= 2) Then '<< FIRST CONDITION
2.If (CStr(TSNStr) = CStr(PTSNStr)) Then '<< SECOND CONDITION
The third condition 第三个条件
(((CInt(TSSEPStr) - CInt(PTSSEPStr)) <= 5) Or ((CInt(TSSEPStr) - CInt(PTSSEPStr)) >= -5)))
always evaluates to false, which should check if difference of TSSEPStr
and PTSSEPStr
is either less than / equal to 5 or greater than / equal to -5. 总是评估为false,这应检查
TSSEPStr
和PTSSEPStr
是否小于/等于5或大于/等于-5。
I want to know if is it possible to use an Or
keyword in a statement that is also used with multiple other And
keywords in VB Script. 我想知道是否可以在与VB脚本中的多个其他
And
关键字一起使用的语句中使用Or
关键字。
With the help of @GaryEvans's answer, I found that this line always evaluated to false because usage of too many parentheses. 在@GaryEvans的答案的帮助下,我发现由于使用了太多的括号,因此该行始终评估为false。
I just made an small change and the following line worked as I expected: 我做了一个小小的更改,并且以下行按预期工作了:
If (CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr)) And (CInt(TSSEPStr) - CInt(PTSSEPStr) <= 5) And (CInt(TSSEPStr) - CInt(PTSSEPStr) >= -5) Then
And made it more shorter and clearer: 并使它更短更清晰:
If (CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr)) And (CInt(TSSEPStr - PTSSEPStr) <= 5) And (CInt(TSSEPStr - PTSSEPStr) >= -5) Then
Yes it is possible, the devil is in the detail, it is all about think of all possible paths and paying close attention to the bracketing which determines the order of evaluation (which from looking at your code, you are well aware of). 是的,这是可能的,细节就是魔鬼,这全都在于考虑所有可能的路径,并密切注意确定评估顺序的括号(通过查看代码,您会清楚地知道)。
The deepest bracketed evaluations are performed first then the next level up until you reach the top. 首先进行最深入的方括号内的评估,然后再进行下一个层次的评估,直到达到顶部为止。 For example (and you can try this in Excel): -
例如(您可以在Excel中尝试):-
5 / 4 * 3 / 2 = 1.875
5 / (4 * 3) / 2 = 0.208333333
The evaluations are:- 评价是:
1.25 * 3 / 2
3.75 / 2
1.875
and 和
5 / 12 / 2
0.416666667 / 2
0.208333333
There is an arithmetic order of precedence that also comes into play here that you may want to read up on. 您可能还需要阅读一下算术优先顺序 。
Also, (and you probably know this too) all elements in an AND
evaluation must be True
for the result to be True
. 另外,(并且您可能也知道),
AND
评估中的所有元素都必须为True
,结果才能为True
。 Any elements in an OR
evaluation that are True
will result in a True
. 在任何元素
OR
评价是True
会导致True
。
True
AND
True
AND
True
= True
True
AND
True
AND
True
= True
True
AND
True
AND
False
= False
True
AND
True
AND
False
= False
True
OR
True
OR
True
= True
True
OR
True
OR
True
= True
True
OR
True
OR
False
= True
True
OR
True
OR
False
= True
You can then also add parenthesis to adjust the order evaluation: - 然后,您还可以添加括号以调整订单评估:-
True
AND
( True
AND
True
) = True
True
AND
( True
AND
True
)= True
True
AND
( True
OR
False
) = True
True
AND
( True
OR
False
)= True
False
OR
( False
AND
True
) = False
False
OR
( False
AND
True
)= False
To your issue, I think you have your greater than and less than mixed up, there was slightly to much bracketing as well but it should have evaluated still. 对于您的问题,我认为您的观点有多有少,也有一些,但应该进行评估。
1 > 2 = False
1> 2 =
False
2 > 1 = True
2> 1 =
True
2 > 2 = False
2> 2 =
False
1 < 2 = True
1 <2 =
True
2 < 1 = False
2 <1 =
False
2 < 2 = False
2 <2 =
False
Lets call CInt(TSSEPStr) - CInt(PTSSEPStr)
i
: - 让我们叫
CInt(TSSEPStr) - CInt(PTSSEPStr)
i
:-
i = 0
(i >= 5) Or (i <= -5) = (False) Or (False) = False
0 is not greater than or equal to 5, 0 is not less than or equal to -5 0不大于5,0不小于-5
i = 10
(i >= 5) Or (i <= -5) = (True) Or (False) = True
10 is greater than or equal to 5, 10 is not less than or equal to -5 10大于或等于5,10不小于或等于-5
This is saying it must be out of the range of -5 to 5. if we flip the greater than/less than operators it says that is must be in the range of -5 to 5, also we need to change Or
to And
. 这是说,它必须是出于 -5 范围为5,如果我们翻转大于/小于运营商也说,这是必须在 -5到5的范围内,我们还需要改变
Or
以And
。
i = 0
(i <= 5) And (i >= -5) = (True) And (True) = True
0 is less than or equal to 5, 0 is greater than or equal to -5 0小于或等于5,0大于或等于-5
i = 10
(i <= 5) And (i >= -5) = (False) And (True) = False
10 is not less than or equal to 5, 10 is greater than or equal to -5 10等于或大于5,10等于或大于-5
If (CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr)) And (CInt(TSSEPStr) - CInt(PTSSEPStr) >= 5) And (CInt(TSSEPStr) - CInt(PTSSEPStr) <= -5) Then
Hope this helps. 希望这可以帮助。
Having many conditions always makes the evaluation tricky because of Precedence and Order of Evaluation. 由于评估的优先级和顺序,拥有许多条件总是会使评估变得棘手。 You might want to review those rules in relation to vb-script.
您可能需要查看与vb-script有关的那些规则。 The solution is usually parentheses like you attempted but I suspect you need one more pair around the first two conditions like:
解决方案通常是像您尝试的那样加上括号,但我怀疑您需要在前两个条件附近再加上一对:
If (((CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr))) And (((CInt(TSSEPStr) - CInt(PTSSEPStr)) <= 5) Or ((CInt(TSSEPStr) - CInt(PTSSEPStr)) >= -5))) Then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.