Appendix E

Control Statements

Control statements tell an application which other statements to execute under a particular set of circumstances.

The two main categories of control statements are decision statements and looping statements. The following sections describe the decision and looping statements provided by Visual Basic .NET.

DECISION STATEMENTS

A decision statement represents a branch in the program. It marks a place where the program can execute one set of statements or another or possibly no statements at all. These include If, Choose, and Select Case statements.

Single-Line If Then

A single-line If Then statement tests a condition and, if the condition is true, executes a piece of code. The code may include more than one simple statement separated by a colon.

Optional Else If clauses let the program evaluate other conditions and execute corresponding pieces of code. A final optional Else clause lets the program execute a piece of code if none of the previous conditions is true.

The syntax for different variations are as follows:

If condition Then statement
If condition Then statement1 Else statement2
If condition1 Then statement1 Else If condition2 Then statement2 _
    Else statement3
If condition Then statement1: statement2
If condition Then statement1: statement2 Else statement3: statement4

Complicated single-line If Then statements can be confusing and difficult to read, so I recommend using the multiline versions if the statement includes an Else clause or executes more than one statement.

Multiline If Then

A multiline If Then statement is similar to the single-line version, except the pieces of code executed by each part of the statement can include multiple lines. Each piece of code ends before the following ElseIf, Else, or End If keywords. In complex code, this format is often easier to read than a complicated single-line If Then statement.

The syntax is as follows:

If condition1 Then
    statements1...
ElseIf condition2
    statements2...
Else
    statements3...
End If

The multiline If statement can contain any number of ElseIf sections.

Select Case

A Select Case statement lets a program execute one of several pieces of code based on a test value. Select Case is equivalent to a long If Then Else statement.

The syntax is as follows:

Select Case test_value
    Case comparison_expression1
        statements1
    Case comparison_expression2
        statements2
    Case comparison_expression3
        statements3
        ...
    Case Else
        else_statements
End Select

A comparison expression can contain multiple expressions separated by commas, can use the To keyword to specify a range of values, and can use the Is keyword to evaluate a logical expression using the test value. The following example’s first case looks for a string in the range “A” to “Z” or “a” to “z.” Its second and third cases look for values less than “A” and greater than “Z,” respectively.

Select Case key_pressed
    Case "A" To "Z", "a" To "z"
        ...
    Case Is < "A"
        ...
    Case Is > "Z"
        ...
End Select

Many developers always include a Case Else section to catch unexpected situations. If every possible situation should be covered by other cases, some developers throw an exception inside the Case Else section to make it easier to find errors.

If and IIf

IIf takes a Boolean value as its first parameter. It returns its second parameter if the value is True, and it returns its third parameter if the value is False.

The syntax is as follows:

variable = IIf(condition, value_if_true, value_if_false)

Note that IIf always evaluates all of its arguments. For example, if the condition is True, IIf only returns the second argument, but it evaluates both the second and third arguments.

The If function does the same thing as IIf except it uses short-circuit evaluation, so it only evaluates the second and third arguments if necessary. If the condition is True in the following code, If evaluates the second argument but not the third:

variable = If(condition, value_if_true, value_if_false)

A second form if the If function takes two parameters: an object reference or a nullable type and a return value. The If function returns the first parameter if it is not Nothing and the second argument if it is Nothing. The following code shows the syntax:

variable = If(nullable_value, default_if_nothing)

IIf and If are often confusing and IIf is slower than an If Then Else statement, so you may want to use If Then Else instead.

Choose

Choose takes an index value as its first parameter and returns the corresponding one of its other parameters.

The syntax is as follows:

variable = Choose(index, value1, value2, value3, value4, ...)

Choose is rarely used, so it can be confusing. To avoid unnecessary confusion, you may want to use a Select Case statement instead.

LOOPING STATEMENTS

A looping statement makes the program execute a series of statements repeatedly. The loop can run for a fixed number of repetitions, run while some condition holds True, run until some condition holds True, or run indefinitely.

For Next

A For Next loop executes a piece of code while a loop control variable ranges from one value to another.

The syntax is as follows:

For variable [As data_type] = start_value To stop_value [Step increment]
    statements
    [Exit For]
    statements
Next [variable]

For Each

A For Each loop executes a piece of code while a loop control variable ranges over all of the items contained in a group such as a collection or array.

The syntax is as follows:

For Each variable [As object_type] In group
    statements
    [Exit For]
    statements
Next [variable]

The group in this code can also be a LINQ (Language Integrated Query) query. The following code creates a LINQ query to select information from the book_data array and then uses a For Each loop to display the results:

Dim book_query = From book_info In book_data
    Select book_info
    Where book_info.Year > = 2000
    Order By book_info.Year
For Each bi In book_query
    Debug.WriteLine(bi.Title)
Next bi

For more information about LINQ, see Chapter 20, “LINQ.”

Do Loop

Do Loop statements come in three forms. First, if the statement has no While or Until clause, the loop repeats infinitely or until the code uses an Exit Do, Exit Sub, or some other statement to break out of the loop.

The syntax is as follows:

Do
    statements
    [Exit Do]
    statements
Loop

The other two forms of Do Loop statements execute as long as a condition is True (Do While condition) or until a condition is True (Do Until condition).

The second form of Do Loop statement tests its condition before it executes, so the code it contains is not executed even once if the condition is initially False.

The syntax is as follows:

Do {While | Until} condition
    statements
    [Exit Do]
    statements
Loop

The third form of Do Loop statement tests its condition after it executes, so the code it contains is executed at least once even if the condition is initially False.

The syntax is as follows:

Do
    statements
    [Exit Do]
    statements
Loop {While | Until} condition

While End

The While End loop executes a series of statements as long as a condition is True. It tests its condition before it executes, so the code it contains is not executed even once if the condition is initially False.

The syntax is as follows:

While condition
    statements
    [Exit While]
    statements
End While

This statement is equivalent to the Do Loop:

Do While condition
    statements
    [Exit Do]
    statements
Loop
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.124