vb.net Conditional Branch Statement

Source: Internet
Author: User
Tags expression

First, simple if condition statement
A conditional expression in a special statement block called a branching structure controls which statements in the program are executed and what execution order is performed. "If ..... Then The branch structure calculates the condition value in the program, and determines the next action to take depending on the criteria value. The simplest "If ..." Then "Branch structure can be written only on one line:
If Condition Then Statement[else Statement]

Note: "If ... The Then branch structure is used to add logical control to a program. Here, "Condition" is a conditional expression, and "Statement" is a valid Visual Basic statement. For example:
If score>=20 Then label1.text= "You win!" is a branching structure that uses the following conditional expression: score>=20
Based on the value of this expression, the program decides whether to set the Label1 object's "Text" property to "you win!". If the value of the "Score" variable is greater than or equal to 20,visual basic sets the value of the property, Visual Basic skips the assignment statement, and then executes the next line in the event procedure. The result of such a comparison operation is not "True" or "False", and the conditional expression never produces an ambiguous value.
Two, If ... Then ... Else Statement
Visual Basic also supports the "If ..." form in another format. Then "Branch structure that contains several conditional expressions, consisting of a multiline statement that contains the important keyword" ElseIf "," Else ", and" End If ".

If Condition1 Then
   statements
ElseIf Condition2 Then
   Statements
[other ElseIf clauses and their corresponding execution statements]
else
   Statements
End If

In this structure, "Condition1" is first computed. If the value of this conditional expression is "true", then the statement under the conditional expression is executed, and if the value of the first condition is not "true", the value of the second expression (Condition2) is evaluated, if the value of the second condition is "true", Then the statement block under the conditional expression is executed (if more conditions are to be judged, then continue to add the "ElseIf" clause and the statement block under the clause, and if all the conditional expression values are not "True", execute the block of statements under the "Else" clause; Finally, the entire structure uses "end If The keyword ends. Multi-line "If ..." Then "structure is particularly suitable for piecewise computational problems, such as tax and fee calculations. The following code shows how to use multiple lines "If ..." Then "structure to determine the problem of progressive tax calculation (income and tax rates are derived from the U.S. Domestic Income Service 1997-Year tax scale):

If adjustedincome<=24650 Then
   ' 15% tax segment
   taxdue=adjustedincome*0.15
ElseIf adjustedincome<=59750 Then
   ' 28% tax segment
   taxdue=3697+ ((AdjustedIncome-24650) *0.28)
ElseIf adjustedincome<=124650 Then
   ' 31% Tax Segment
   taxdue=13525+ ((AdjustedIncome-59750) *0.31)
ElseIf adjustedincome<=271050 Then
   ' 36% tax segment
   taxdue=33644+ ((AdjustedIncome-124650) *0.36)
Else
   ' 39.6% tax segment
   taxdue=86348+ ((AdjustedIncome-271050) *0.396) End
If

Note: 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, you can use the Select cases to determine the structure.

Third, Select case structure
Visual Basic also supports the use of the "Select case" branch structure in a program to control the execution of statements. "Select Case" Structure and "If ..." Then ... Else structure is similar, but more efficient when dealing with a branch that relies on a key variable or is called a test case. Also, using the Select Case structure can improve the readability of your program. The syntax for the Select Case structure looks like this:

Select Case Variable Case
     Value1
         statements Case
     Value2
         statements Case
     Value3
         statements
     ...
End Select

The Select Case structure begins with the keyword "Select Case" and ends with the keyword "End Select". The "Variable" in the Select Case structure can be a variable, a property, or an expression, "Value1", "Value2", "Value3" can be numeric, string, or other value related to other cases to be tested, and if one of these values matches a variable, the The statement under the case clause is executed, and Visual Basic executes the statement following the end Select statement. You can use any number of "case" clauses in the Select Case structure, and the case clause can also include multiple "value" values, separated by commas between multiple "value" values.
The following example shows how to use the "Select case" structure in a program to print information about someone's age. When the "age" variable matches a "case" value, the corresponding information is displayed in the Label object.

Select Case age Case
    label1.text= "You can drive now!"
Case of
    label1.text= "You can vote now!"
Case of
    label1.text= "can drink wine with your meals."
Case
    label1.text= "time to retire and have fun!"
End Select

Note: the "Select case" structure is more functional than the "If ..." Then "structure is clearer and easier to read. The Select Case structure also supports the "case Else" clause, which can be used to display information when all "case" conditions are not met. Here is an example that describes the use of the case ELSE clause:

Select Case age Case
    label1.text= "You can drive now!"
Case of
    label1.text= "You can vote now!"
Case of
    label1.text= "can drink wine with your meals."
Case
    label1.text= "time to retire and have fun!"
Case Else
    label1.text= "You ' re a great age! Enjoy it! "
End Select

Note: The Select Case structure always evaluates the value of the expression at the beginning, and the "If ..." Then ... ELSE structure evaluates different expressions for each "ElseIf" statement, and only if the "if" statement evaluates the same expression as each "ElseIf" statement can use the "Select case" structure to replace the "if ..." 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.