Learning the Visual Basic .NET Language - Conditional Structures
(Page 7 of 9 )
In many ways, conditional logic—deciding which action to take based on user input, external conditions, or other information—is the heart of programming.
All conditional logic starts with a condition: a simple expression that can be evaluated to True or False. Your code can then make a decision to execute different logic depending on the outcome of the condition. To build a condition, you can use any combination of literal values or variables, along with logical operators. Table 3-7 lists the basic logical operators.
Table 3-7. Logical Operators
Operator | Description |
= | Equal to |
<> | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
And | And (evaluates to True only if both expressions are True) |
Or | Or (evaluates to True if either expression is True) |
You can use the comparison operators (<, >, <=, >=) with numeric types and strings. A string is deemed to be “less than” another string if it occurs earlier in an alphabetic sort. Thus “apple” is less than “attach.”
The If … End If Block The If … End If block is the powerhouse of conditional logic, able to evaluate any combination of conditions and deal with multiple and different pieces of data. Here’s an example with an If … End If block that features two conditions:
If MyNumber > 10 Then
' Do something.
ElseIf MyString = "hello" Then
' Do something.
Else
' Do something.
End If
Keep in mind that the If … End If block matches one condition at most. For example, if MyNumber is greater than 10, the first condition will be met. That means the code in the first conditional block will run, and no other conditions will be evaluated. Whether or not MyString contains the text “hello” becomes irrelevant, because that condition will not be evaluated.
The Select Case Block VB .NET also provides a Select Case structure that you can use to evaluate a single variable or expression for multiple possible values. In the following code, each case examines the MyNumber variable, and tests if it’s equal to a specific integer.
Select Case MyNumber
Case 1
' Do something if MyNumber = 1.
Case 2
' Do something if MyNumber = 2.
Case Else
' Do something if MyNumber is anything else.
End Select
If desired, you can handle multiple cases with one segment of code by including a list of comma-separated values in the Case statement.
Select Case MyNumber
Case 1, 2
' Do something if MyNumber = 1 Or MyNumber = 2.
Case Else
' Do something if MyNumber is anything else.
End Select
Unlike the If … End If block, Select Case is limited to evaluating a single piece of information at a time. However, it provides a leaner, clearer syntax than the If … End If block for situations in which you need to test a single variable.
Next: Loop Structures >>
More Visual Basic.NET Articles
More By Apress Publishing
|
This article is excerpted from chapter three of the book Beginning ASP.NET in VB.NET: From Novice to Professional, written by Matthew MacDonald (Apress, 2004; ISBN: 1590592786). Check it out at your favorite bookstore today. Buy this book now.
|
|