2.8. Statements


As in VB 6, a complete program instruction in VB 2005 is called a statement. Programs consist of sequences of statements. You end each statement with a carriage return.

In VB 2005, spaces, tabs, and carriage returns (newlines) are considered to be "whitespace." Extra whitespace is ignored in VB 2005, as in VB 6, a feature that many consider an endearing (and forgiving) quality of the language.

2.8.1. Decision-Making (Branching) Statements

VB 2005 retains the traditional VB 6 statements for decision making but adds a few new wrinkles of its own. Decision-making statements fall into two categories:

  • If-Then-Else

  • Select-Case

2.8.1.1. If-Then-Else

Just as in VB 6, in VB 2005, you make decisions using the If-Then-Else construct.

 If <condition> Then        <statement(s)>     Else        <statement(s)>     End if  

Here is a short example:

 Dim day As Short = 4 Dim dayofWeek As String If day = 1 Then        dayofWeek = "Monday"     End If 

In the preceding code, if day is equal to 1, the string "Monday" is then assigned to the dayofWeek variable. For a one-line statement, you can shorten the above code to:

 If day = 1 Then dayofWeek = "Monday" 

However, if you have multiple statements to execute if a condition is met, use of the End If statement is mandatory. VB 2005 lets you pack a block of conditional code into a single line. For example, the following block of code:

 If day = 1 Then         dayofWeek = "Monday"         currentTime = Now     End If 

is equivalent to this single line of code:

 If day = 1 Then dayofWeek = "Monday" : currentTime = Now 

The grouping of several statements into a single line using the : character, as shown in the preceding snippet, is useful in cases where you want to group multiple statements into a single line to improve the readability of your code. The grouping feature is also useful for organizing a related group of variables.

You can also nest several If-Then-Else statements, as shown in Example 2-1.

Example 2-1. Nesting If-Then-Else statements
 Dim day As Short = 4 Dim dayofWeek As String If day = 1 Then    dayofWeek = "Monday" Else     If day = 2 Then       dayofWeek = "Tuesday"    Else       If day = 3 Then          dayofWeek = "Wednesday"       Else          If day = 4 Then             dayofWeek = "Thursday"          Else             If day = 5 Then                dayofWeek = "Friday"             Else                If day = 6 Then                   dayofWeek = "Saturday"                Else                   If day = 0 Then                      dayofWeek = "Sunday"                   Else                      Msgbox("Number out of range")                   End If                End If             End If          End If      End If    End If End If 

Note the matching End If statement for each If statement. If you have multiple nested If-Then-Else constructs, you can simplify the above code using the ElseIf keyword (also supported in VB 6), as shown in Example 2-2.

Example 2-2. Using the ElseIf keyword
 If day = 1 Then     dayofWeek = "Monday" ElseIf day = 2 Then     dayofWeek = "Tuesday" ElseIf day = 3 Then     dayofWeek = "Wednesday" ElseIf day = 4 Then     dayofWeek = "Thursday" ElseIf day = 5 Then     dayofWeek = "Friday" ElseIf day = 6 Then     dayofWeek = "Saturday" ElseIf day = 0 Then     dayofWeek = "Sunday" Else     MsgBox("Number out of range") End If 

Note that if you use the ElseIf keyword, the number of End If statements is reduced to one (in this example).

Short-Circuiting

Short-circuiting is a compiler optimization technique that reduces the checking of redundant conditions in a decision-making statement. In both the logical And and Or operations, both conditions are evaluated regardless of their results. To short-circuit the And operation, you can replace it with the new AndAlso operator so that if the first condition is false, the second condition is not evaluated.

Likewise, to short-circuit the Or operations, you can use the OrElse operator. If the first condition evaluates to true, the second condition is not evaluated.

VB 6 does not support short-circuiting when evaluating an expression. Hence, in order not to break existing code, Microsoft added the AndAlso and OrElse operators in VB 2005 for short-circuiting.


2.8.1.2. Select…Case

If you have multiple conditions to test, it is often much easier (and more readable) to use the Select…Case construct. Example 2-3 shows a rewrite of the previous code segment using the Select…Case construct.

Example 2-3. Using the Select…Case statement
 Select Case day    Case 1 : dayofWeek = "Monday"    Case 2 : dayofWeek = "Tuesday"    Case 3 : dayofWeek = "Wednesday"    Case 4 : dayofWeek = "Thursday"    Case 5 : dayofWeek = "Friday"    Case 6 : dayofWeek = "Saturday"    Case 0 : dayofWeek = "Sunday"    Case Else : Msgbox( _        "Number out of range") End Select 

2.8.2. Looping (Iteration) Statements

VB 2005 provides several looping constructs. They are all supported in VB 6 as well, unless otherwise noted:

 For For-Each While Do-While Do-Until 

Each of the following examples (Example 2-4 through Example 2-8) prints a series of array members with indexes ranging from 0 to 5 using one of the looping constructs supported by VB 2005.

Example 2-4. Using the For loop
 Dim num() As Integer = {1, 2, 3, 4, 5, 6} For n as Integer = 0 To 5     Console.Write(num(n)) Next 

VB 6 Tip: In VB 6, you need to declare the loop variant (n) in a separate statement. Only VB 2005 allows you to declare it and use it at the same time.


Example 2-5. Using the For-Each loop
 Dim num() As Integer = {1, 2, 3, 4, 5, 6} For Each i As Integer In num     Console.Write(i) Next 

Example 2-6. Using the While loop
 Dim num() As Integer = {1, 2, 3, 4, 5, 6} Dim j As Integer = 0 While j <= 5     Console.Write(num(j))     j += 1 End While 

VB 6 Tip: In VB 6, you use the While-Wend statement to implement a While loop.


Example 2-7. Using the Do-While loop
 Dim num() As Integer = {1, 2, 3, 4, 5, 6} Dim k As Integer = 0 Do While k <= 5     Console.Write(num(k)) k += 1 Loop 

Example 2-8. Using the Do-Until loop
 Dim num() As Integer = {1, 2, 3, 4, 5, 6}  Dim m As Integer = 0 Do     Console.Write(num(m))     m += 1 Loop Until m > 5 



Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net