|
-
Nov 1st, 2000, 08:09 AM
#1
Thread Starter
Lively Member
I was curious to see if I could have multiple condidtions in an if statment, like:
If X=4 & Y>3 & MyString "hello" Then
Is something like that possible? I've looked through a few box and in the online help, and I can't see anything like that. (would be nice)
Thanks for any infor
-
Nov 1st, 2000, 08:12 AM
#2
Guru
Use the And keyword. 
Code:
If X = 4 And Y > 3 And MyString = "hello" Then
Also check out the Or keyword.
(Note: There's also Eqv, but it's rarely used)
-
Nov 1st, 2000, 08:13 AM
#3
Thread Starter
Lively Member
I was playing around and this seems to work like I wanted:
----------
X = 1
Y = 2
If X = 1 And Y = 2 Then
MsgBox "YES"
End If
----------
Is there anything else I should know about doing something like this?
Thanks!
-
Nov 1st, 2000, 08:31 AM
#4
Well ...
From the number of your posts, you have had quite some experience in VB, and I assume that you have some basic knowledge of programming.
With this in mind, you can be sure that the logical operators AND, OR and NOT work the same way in VB, too.
Apart from that, there is nothing special to know about checking multiple conditions, except that use of parentheses will enhance the readability and avoid any pitfalls in using the above 3 operators.
And by the way, if you are going to check for multiple EQUALITIES, i.e.
Code:
If A = 5 Or A = 6 Or A = 7 Then
MsgBox "That's it!"
Endif
then you would be better off writing something like:
Code:
Select Case A
Case 5, 6, 7
MsgBox "That's it!"
End Select
-
Nov 1st, 2000, 08:50 AM
#5
Thread Starter
Lively Member
Yep, I had never touched VB until about 3 weeks ago.
I've had two Basic classes in my life, as well as Assembly Laungage programing (all the way up to advanced programs), and I've done some VB script work for Terminal programs.
Having the logic already working in my brain has helped out with the learning of VB... I seem to be picking it up really fast (with the help of this forum and the books I bought).
Actually, I think I was something like 10 (?) when the old Black Case Apple's first came out.... For some reason my mother sent me through an "open student" summer class on the BASIC language (which was taught on those original black Apples). I'm really glad she did that - core BASIC knowledge sunk into my little young brain. 
Anyway - I had an idea to do something which seemed impossible at first. Now, I'm almost done, and in about three weeks I feel pretty comfortable with VB. I'm really loving it.....
I really appreciate all the help (more stupid questions to come).
-
Nov 1st, 2000, 08:53 AM
#6
Guru
Well... Alright, I'll give you more information about this. 
Code:
If Condition1 And Condition2 [And Condition3...] Then
This requires that all the conditions are True.
Code:
If Condition1 Or Condition2 [Or Condition3...] Then
This requires that at least one of the conditions is True.
For example:
Code:
If True And True And True Then
' This will execute
End If
If False And False And True Then
' This won't execute
End If
If False Or True Or False Then
' This will execute
End If
If False Or False Or False Then
' This won't execute
End If
It gets confusing now, and the facts above are the most important ones. So, reading the rest of this post can be dangerous. 
Anyways... When you do this:
It checks whether Something is non-zero.
False translates to zero, and True translates to -1.
Code:
' This:
X = 1
If X = 1 Then
' Is the same as:
If True Then
' Which is the same as:
If -1 Then
' Since -1 is non-zero, the statements inside the If block are executed.
' This:
X = 1
If X > 7 Then
' Is the same as:
If False Then
' Which is the same as:
If 0 Then
' Since 0 is zero, the statements inside the If block aren't executed.
' This:
If X Then
' Means:
If Not X = 0 Then
' Or:
If X <> 0 Then
' Note: From here on, it gets confusing. :rolleyes:
' Here is a common mistake. This:
If Not X Then
' Does NOT mean:
If X = 0 Then
' Instead, it means... "If you perform a bitwise NOT on X, is the result nonzero?"
' Which means:
If (Not X) <> 0 Then
' This:
If X And Y Then
' Means:
If (X <> 0) And (Y <> 0) Then
' Now, lets say X is nonzero, but Y is zero.
If True And False Then
If -1 And 0 Then
' The bitwise AND is unsigned, so this equals:
If &HFFFFFFFF And 0 Then
' The result of "FFFFFFFF And 0" is zero.
If 0 Then
' The same is with Or, except it performs a bitwise Or, instead of a bitwise And.
' The result of "FFFFFFFF Or 0" is "FFFFFFFF" which is -1.
If -1 Then
If you understood the last part, your situation is very good.
If you didn't, don't worry... Nobody ever really understands it. 
Here is Eqv, which is rarely used, so I won't expand on it:
Code:
' 1) Logical Eqv
' This:
If X Eqv Y Then
' Means:
If (X And Y) Or Not (X Or Y) Then
' Which is equivalent to:
If (X And Y) Or (Not X And Not Y) Then
' 2) Bitwise Eqv
' Bit1, Bit2 and Bit3 are single bits (0 or 1).
' This:
Bit3 = Bit1 Eqv Bit2
' Means:
If Bit1 = Bit2 Then Bit3 = 1 Else Bit3 = 0
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|