Selection
|
|
Versions Selection is implemented in one of three ways:
The If/Then structure is called a single-selection structure because it selects or ignores a single action. The If/Then/Else structure is called a double-selection structure because it selects between two different actions. |
|
|
|
A selection structure is used to choose among alternative courses of action. Example: The pseudocode statement If student's grade is greater than or
equal to 60 determines if the condition "student's grade is greater than or equal to 60" is True or False. If the condition is True, then "Passed" is displayed, and the subsequent statement is performed. If the condition is False, the display statement is ignored, and the next pseudocode statement in order is performed. The preceding pseudocode If/Then statement may be written in Visual Basic as If grade >= 60 Then lblStatus.Text = "Passed" OR If grade >= 60 Then The If/Then selection structure, when written on a single line, normally expects only one statement in its body. To include several statements in the body of an If/Then, write the statements between the If/Then and End If keywords. A control structure's multiple statement body is often called a block. Multiple-line If/Then or If/Then/Else statements must terminate with End If. Example: If grade >= 60 Then The figure below illustrates the single-selection If/Then structure.
|
|
|
|
If/Then/Else Selection Structure The If/Then/Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False. Example: The pseudocode statement If student's grade is greater than or
equal to 60 displays "Passed" if the student's grade is greater than or equal to 60 and displays "Failed" if the student's grade is less than 60. In either case, after the message is displayed, control passes to the statement following the If/Then/Else. Note that the body statements of selection structures are indented.
The preceding pseudocode If/Then/Else structure may be written in VB as If grade >= 60 Then Note that each If/Then or If/Then/Else spanning multiple lines must end in End If unless the line-continuation character _ is used. The line continuation character must be preceded by a blank. The last End If always matches up with the previous If/Then or If/Then/Else. The following figure shows the If/Then/Else selection structure.
|
|
|
|
Nested Ifs Nested If/Then/Else structures test for multiple cases by placing If/Then/Else structures inside If/Then/Else structures. Example: If student's grade is greater than or equal to 90
If grade >= 90 Then
If grade is greater than or equal to 90, the first four conditions will be True, but only the statement after the first test will be executed. After the assignment is executed, the Else-part of the "outer" If/Then/Else statement is skipped, in fact skipping the entire remainder of the preceding code segment. The preceding If/Then/Else structure can also be written as follows: If grade >= 90 Then Both forms are equivalent. |
|
|
|
For more applications of the Selection structure... ...see the Error Checking notes. validation
|
|
|
|
Select Case Multiple-Selection Structure The syntax of the Select Case is as follows: Select Case variable
Select Case Types The Select Case statement can be used with all variable types: Dim someVariable As String
Select Case Ranges You can use the keyword To to specify ranges in the Case expressions. Dim someVariable As Integer
If you want to execute the same block of code for different values of the test expressions, you can do so by specifying the non-consecutive values, separated by commas. Select Case someVariable
The Is Keyword Keyword Is along with the comparison operators to specify a range of values to test. Select Case someVariable
Combining Ranges To combine more than one range of values in a single Case statement, you can use the logical operators And and Or. Select Case NumVar
Nested Select Case Statements You can nest Select statements, but each Case has a corresponding End Select. Select Case
someVariable Grade example using a Select Case Select Case grade |
|
|
|
More See also Selection Functions. |
|
|