I l @ ve RuBoard |
In VB.NET, statements represent executable code. The next section outlines the statements found and VB.NET and their usage. BlocksA block statement allows multiple statements to be written in contexts where a single statement is allowed. A block statement consists of a statement list enclosed by braces: Sub Main End Sub Statement ListsA statement list is simply one or more statements written in sequence and enclosed in blocks as outlined following: Sub Main Refresh() Repaint() End Sub LabeledA labeled statement allows a statement to be referenced by a Goto statement. The scope of the label is the block that encloses it, including any nested blocks: Sub Main If (X>0) Then Goto MyLabel X = X + 1 MyLabel: return X End Sub DeclarationsA declaration statement declares a local variable or constant: Dim X as Integer = 3 Dim Const pi As Decimal = 3.14 ExpressionsAs expected, an expression statement evaluates a given statement: Return (x * y) Repaint(Me) SelectionSelection statements allow for the execution of one of a list of possible statements based upon the value of a controlling expression. Selection statements include the If...Then...Else and Select...Case statements. IfThe If statement selects a statement for execution based on a Boolean expression: If (x > 0) Then x = x -1 End If This statement may be coupled with an else clause in order to specify a statement for execution if the condition fails: If (x > 0) Then x = x - 1 Else return x End If If statements may also be nested: If (x <> 3) Then If (x > 0) Then x = x - 1 Else Return x End If End If Select CaseTo provide multiple statements for possible execution based upon the value of a condition, we use the Select Case statement: Select Case (x) Case 1 MessageBox.Show("X = 1") Case 2 MessageBox.Show("X = 2") Case 3 MessageBox.Show("X = 3") Case Else MessageBox.Show("X does not equal 1,2, or 3") End Select The Select...Case statement evaluates the expression and looks for a match with a constant value from one of the labeled statements. When a match is found, execution control is transferred to the statement list for the matching label. If no match is found, control is transferred to the Case Else block and its statement list. LoopsLoop statements allow for the execution of a statement list as long as some expression evaluates True . VB.NET includes four such iteration statements: While...End While , Do...Loop , For...Next , and For Each...Next . While...End WhileThe While...End While statement executes a statement list zero or more times, depending on the value of the conditional expression: While (x < 100) MessageBox.Show(x) X = X + 1 End While Do...LoopSimilar to the While statement, the Do...Loop statement executes one or more times: Do MessageBox.Show(x) X = X + 1 Loop While(X < 100) Note that the Do...Loop statement can use the While keyword as used above, which breaks the loop if the expression evaluates to False or the Until keyword, which breaks the loop if the expression evaluates to True. For...NextThe For...Next statement loops on a set of bounds. The statement specifies a lower bound, an upper bound, and an optional step for the iteration. Upon commencement of the loop, the three values are evaluated. If the step is omitted, the iteration defaults to a step of 1 : For x = 0 to 100 MessageBox.Show(x) Next For x = 100 to 0 Step 1 MessageBox.Show(x) Next For Each...NextVery similar to the For...Next statement, the For Each...Next statement enumerates a collection and executes the statement list once for each item in the collection: For Each x in Args MessageBox.Show(x) Next Control-Flow StatementsAnother type of statement, the control-flow statement allows the unconditional transfer of control execution in a VB.NET program. These statements include Goto , Exit , Stop , Return , and End statements. GotoThe Goto statement transfers control to the statement matching the specified label: Sub Main If (X>0) Then Goto MyLabel X = X + 1 MyLabel: return X End Sub ExitAn Exit statement transfers control to the next statement after the end of the enclosing block statement. If the block is a method, execution is transferred back to the caller of the method. Do x = x + 10 If x > 35 Then Exit Do Loop While (x > 0) ReturnThe Return statement returns control to the caller of the procedure in which the Return statement appears. If enclosed within a function, the Return statement may take a return value argument: Function GetStudentID(FullName As String) As Integer Return "Jim Smith" End Function StopUsing the Stop statement permits putting breakpoints in your code: Dim x As Integer For x = 1 To 5 Debug.WriteLine (x) Stop 'Stop during each iteration. Next I EndSimilar to the Stop statement, the End statement terminates program execution and can only appear in executables (.exes) and not libraries (. dlls ). TryThe try statement provides exception handling during execution of a statement block. The try block may be followed by one or more catch blocks and may be followed by one finally block: Try Repaint() Catch (e As Exception) 'handle gracefully If(x < 0) Then ' out of range Throw New Exception("Index out of Range") End If Finally ' any clean-up code here End Try |
I l @ ve RuBoard |