This tutorial explains in depth how Visual Basic 6 handles conditional (if) statements and different loops. It goes very in depth so if this is your first time programming, it may be a little advanced. Just try to understand as best you can and as you learn more it will make more sense.
Up until now, you have learned how to develop VB6 applications that store values, perform mathematical computations on these values, and display the results of these computations--all of which is swell and dandy, but no more useful than your average calculator. What if you want, for instance, to determine if a number is odd, to find the factorial of a number, or to generate the Fibonacci Series? None of these tasks can be done through simple arithmetic, but rather they require something known as control structures.
Control structures in Visual Basic take two primary forms: if-statements and loops. With an if-statement, you can evaluate an expression, and perform one task if that expression is True and perform another if it's False. This allows your program to respond differently to different values and inputs, which allows for far more advanced calculations and a far wider range of possible behaviors. Loops, on the other hand, allow you to repeat the same task a given number of times, making it possible to go far beyond basic arithmetic to achieve goals such as calculating the Fibonacci series.
Logical Expressions and Operators
Before we can proceed to learning about if-statements, we must first learn about logical expressions in Visual Basic. In Variables and Types in VB6 you learned about a type of variable called a Boolean, which has only two possible values: True and False. A Boolean value can be defined explicitly using bl = True, but this usage of Booleans is rare and not particularly useful. The more common application of a Boolean is in evaluating a logical expression, any expression which can be evaluated as either True or False.
A typical logical expression in Visual Basic takes the following form: = P Q. P and Q can be either constants, variables, or logical expressions themselves. The is a logical or comparative operator, anything that performs a comparison between P and Q and returns a Boolean value. Here's an example of a logical expression in Visual Basic:
Public Sub Main() Dim B As Boolean Dim P As Integer, Q As Integer P = 5 Q = 7 B = (P = Q) MsgBox B End Sub
In this example, we used the logical operator of equality, denoted by the "=" sign. This operator returns True if P is equal to Q and False if it does not. In this example, B is False because P (5) does not equal Q (7). The parentheses, though not needed, are placed around P = Q to make clear that this is a logical expression, rather than some form of assignment.
Now, let's take a look at a few other logical operators in Visual Basic. Please note that these are not anywhere near all of the comparison and logical operators available in Visual Basic, but they are the ones you'll use most frequently.
The following operators are all comparison operators, meaning that they compare one expression to another and return a Boolean. They are all overloaded, meaning that their left-hand and right-hand expressions can be of (almost) any type.
As you saw in the example above, the equality operator is denoted by an equals sign ("="). This operator can be quite confusing because it is both an assignment operator and a logical operator. The distinction between whether it is acting as a logical or an assignment operator is made solely upon context--if a line begins with a variable, followed by an equals sign, followed by an expression, then the equals sign is treated as an assignment operator, meaning that the value of the following expression is written to the variable. If it is found anywhere else in a program, then it is treated as a logical operator.
Syntax: P = Q
Returns: True if P is equal to Q, and False if it is not.
This operator is synonymous with: (P = Q) = False. In this format, (P = Q) is the left hand expression, and False is the right hand expression, which is evaluated for equality. If (P = Q) is True, then (True = False) is false, but if (P = Q) is True, then (True = True) is True.
Syntax: P <> Q
Returns: True if P is not equal to Q, and False if P is equal to Q.
Greater Than ">"
Syntax: P > Q
Returns: True if P is greater Q, and False if P is less than or equal to Q.
Less Than "<"
Syntax: P < Q
Returns: True if P is less than Q, and False if P is greater than or equal to Q.
Greater Than Or Equal To ">="
Syntax: P > Q
Returns: True if P is greater than or equal to Q, and False if P is less than Q.
Less Than Or Equal To "<="
Syntax: P < Q
Returns: True if P is less than or equal to Q, and False if P is greater than Q.
The following operators are logical operators that can take only Boolean values as arguments; this means that both P and Q can each be only true or false. Keep in mind that the evaluation of every logical expression returns a Boolean, thus P and Q can also be logical expressions.
While this lesson will not go into this in depth, all of these operators can also be used as bitwise operators, in which case they do not require that the expressions be Boolean, nor do they truly return Boolean. For the time being, please only use them as logical operators, as it will save you many headaches.
This operator does not take the standard form of P Q. Instead it is a single-argument operator, that takes only one Boolean expression. The negation operator ("Not") quite simply returns the opposite of the given Boolean expression--it is synonymous with (P = False).
Syntax: Not P.
The Conjunction operator ("And") takes two or more Boolean expressions and returns true iff both expressions are True.
Syntax: P And Q
|P||Q||P And Q|
The Disjunction operator ("Or") takes two or more Boolean expressions and returns true if any of them is True.
Syntax: P Or Q
|P||Q||P Or Q|
Exclusive Disjunction "Xor"
The Exclusive Disjunction, also known as "exclusive or", operator ("Xor") takes two Boolean arguments and returns true if one, and only one, of them is True.
Syntax: P Xor Q
|P||Q||P Xor Q|
As you learned about in Control structures, the If-Then statement is the most simplistic and widely used form of flow control. In short, an If-Statement evaluates a logical expression, performs one task if that expression is True, and does either nothing or performs a seperate task if it is False. And so how do we write one in Visual Basic?
If-Statements in both VB6 and BASIC fall under three categories: the simple If-Then statement, the If-Then-Else statement, and the If-Then-ElseIf statement. If-statements may be nested inside of any other control block, including inside of other If-Statements. Nesting will be covered in depth later.
The If-Then Statement
VB6 uses the same simplistic and easily comprehendible syntax for If-Then Statements as BASIC. The If-Then Statement can be written two ways:
If (Expression) Then (Line of code to execute)
If (Expression) Then
(Multiple lines of code to execute)
The (Expression) parameter can be any Boolean value or logical expression. If the code you want to execute when (Expression) is True can fit on one line, then you can write the entire If-Then statement on one line with no enclosing "End If".
The If-Then-Else Statement
But now what if you want certain code to be executed when the expression you are evaluating is true and certain code to be executed when the expression is false? You could of course write:
If a Then MsgBox "A is True!" If Not a Then MsgBox "A is False!"
But instead of having to write that every time, VB6 provides an additional "Else" clause for your If-Statements. The usage is very simple:
If (Expression) Then
(Code to execute if True)
(Code to execute if False)
One Step Further: ElseIf
Okay, so now that you can perform two different actions depending upon a variable's state, but what if that's just not enough? That's why VB6 also has an ElseIf statement, which has the following syntax:
If (Expression1) Then
(Code to execute if Expression1 is True)
(Code to execute if Expression1 is False and Expression2 is True)
(Code to execute if Expression1 and Expression2 are False, but Expression3 is True)
(Code to execute if neither Expression1, Expression2 nor Expression3 is True)
You can use as many Else-If statements as you want, though you may have no more than one Else statement for every If statement.
So now that we know everything we need to know to begin using If-Statements in our code, let's look at some examples.
Public Sub Main() Dim S1 As String, S2 As String S1 = "Foo" S2 = "BAR"
Dim X As Integer
X = 0
If X < 5 Then Debug.Print "X is less than 5"
If X > 3 Then
Debug.Print "X is greater than 3."
X = 3
Debug.Print "The value of X was changed from 0 to 3"
If S1 = "FOO" Then
If S2 <> "BAR" Then
Debug.Print "S2 is not BAR."
ElseIf (X > 0 And X <= 3) And (S2 = "BAR") Then
Debug.Print "X is greater than 0 and less than or equal to 3, and S2 = 'BAR'."
ElseIf X = 3 Then
Debug.Print "X is 3 and S2 is not BAR."