VB variables, constants and data types and process overview (12)

Source: Internet
Author: User
Tags case statement integer variables

If ... Then ... Else
Use If ... Then ... The Else block defines a few statement blocks and executes one of the statements:
If Condition1 Then
[Statementblock-1]
[ElseIf condition2 Then
[Statementblock-2]] ...
[Else
[Statementblock-n]]
End If
Visual Basic tests Condition1 first. If it tests condition2 for False,visual Basic, and so on, until you find a condition that is True. When it finds a condition that is true, Visual Basic executes the corresponding block of statements and then executes the code following end If. As a selection, you can include the Else statement block, or VisualBasic execute the ELSE statement block if none of the conditions are True.
If ... Then ... ElseIf just If ... Then ... A special case of Else. Note that you can use any number of ElseIf clauses, or none. You can have an ELSE clause, regardless of whether there is a ElseIf clause.
For example, an application can take action based on which control in the menu control array is clicked:
Private Sub Mnucut_click (Index as Integer)
If Index = 0 Then ' cut ' command.
Copyactivecontrol ' invokes a generic procedure.
Clearactivecontrol
ElseIf Index = 1 Then ' copy ' command.
Copyactivecontrol
ElseIf Index = 2 Then ' purge ' command.
Clearactivecontrol
Else ' paste ' command.
Pasteactivecontrol
End If
End Sub
Note that you can always add more ElseIf blocks to the If ... Then structure. However, when each elseif compares the same expression to a different number, the structure is tedious to write. In this case, Select cases can be used to determine the structure.
For more information, see the "If ..." in the Visual Basic 6.0 Language Reference manual. Then ... Else statement ".

Select Case
Visual Basic provides a Select Case structure instead of If ... Then ... else so that one can be selectively executed in multiple statement blocks. The ability to Select case statements and If ... Then ... Else statements are similar, but for multiple selections, the Select Case statement makes the code more readable. The
Select case processes a test expression above the structure and evaluates it only once. VisualBasic then compares the value of an expression with the value of each case in the structure. If equal, the statement block associated with the case is executed.
Select Case testexpression
[case Expressionlist1
[statementblock-1]]
[Case Expressionlist2
[ Statementblock-2]]
.
.
.
[Case Else
[statementblock-n]]

End Select
Each expressionlist is a list of one or several values. If you have more than one value in a list, separate the values with commas. Each statementblock contains 0 or more statements.
If more than one case matches the test expression, only the statement block associated with the first matched case is executed. If there is no value in the expression list that matches the test expression, Visual Basic executes the statement in the case ELSE clause (which is optional).
For example, suppose in the If ... Then ... Else example, to add a command to the Edit menu. You can add a ElseIf clause, or use a Select case to write a function:
Private Sub Mnucut_click (Index as Integer)
Select Case Index
Case 0 ' cut ' command.
Copyactivecontrol ' invokes a generic procedure.
Clearactivecontrol
Case 1 ' copy ' command.
Copyactivecontrol
Case 2 ' clear ' command.
Clearactivecontrol
Case 3 ' paste ' command.
Pasteactivecontrol
Case Else
Frmfind.show ' Displays the found dialog box.
End Select
End Sub
Note that the Select case structure evaluates the value of an expression at the beginning of each time. and If ... Then ... The else struct computes a different expression for each ELSEIF statement. The Select case structure can only be substituted for the IF if statement and each ElseIf statement evaluates the same expression ... Then ... Else structure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.