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) StatementsVB 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:
2.8.1.1. If-Then-ElseJust 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 statementsDim 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 keywordIf 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).
2.8.1.2. Select…CaseIf 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 statementSelect 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) StatementsVB 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 loopDim num() As Integer = {1, 2, 3, 4, 5, 6} For n as Integer = 0 To 5 Console.Write(num(n)) Next
Example 2-5. Using the For-Each loopDim 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 loopDim 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
Example 2-7. Using the Do-While loopDim 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 loopDim 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 |