GTAVLover
GTAVLover

Reputation: 1427

How to use "And" and "Or" keywords together in a same "If" statement

I'm trying to use And and Orkeywords on a single If statement in my VB Script, which I use with VLC Media Player to stream using command line. 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 ((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.

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.

Upvotes: 3

Views: 6968

Answers (3)

GTAVLover
GTAVLover

Reputation: 1427

With the help of @GaryEvans's answer, I found that this line always evaluated to false because usage of too many parentheses.

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

Upvotes: 2

Gary Evans
Gary Evans

Reputation: 1890

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): -

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. Any elements in an OR evaluation that are True will result in a True.

True AND True AND True = True

True AND True AND False = False

True OR True OR True = 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 OR False) = True

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

2 > 1 = True

2 > 2 = False

1 < 2 = True

2 < 1 = False

2 < 2 = False

Lets call 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

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

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.

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

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

If (CurrentEP >= 2) And (CStr(TSNStr) = CStr(PTSNStr)) And (CInt(TSSEPStr) - CInt(PTSSEPStr) >= 5) And (CInt(TSSEPStr) - CInt(PTSSEPStr) <= -5) Then

Hope this helps.

Upvotes: 2

sirgatsen
sirgatsen

Reputation: 13

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. 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

Upvotes: 0

Related Questions