3.4.1 Branch Structure

Source: Internet
Author: User

The branch structure is used to determine and branch the statement to be executed based on the result True or False. VB. NET provides two condition statements for implementing the branch structure: If statement and Select... Case statement.

1. If statement
An If statement is often used in programming. It determines whether a conditional expression is true and determines the statements to be executed by certain programs based on whether the conditional expression is true or not.
(1) If... Then... Else statement
Syntax:
If condition expression Then
Statement Block 1
[Else
Statement Block 2]
End If
[] In the syntax format indicates that the content is optional. The execution process of this If statement is: first, judge whether the conditional expression is true or false. If it is true, execute statement Block 1 after Then; If it is false, the statement Block 2, 3-3a after Else is executed. If the Else clause is omitted and the value of the conditional expression is false, the statement after the conditional statement is executed is skipped, as shown in 3-3B.

 

Figure 3-3 If statement execution Flowchart

For example:
If x S = x' when x End If
Another example is:
If a> B Then
Max =
Else
Max = B 'when End If
(2) nesting of If Statements
The If statement itself is also a statement, which can also be a statement in other If statements to form an If statement that contains an If statement, known as If statement nesting. Because the If statement can appear in both the If clause and the Else clause, there are multiple forms of If statement nesting. One of the forms is given below.
If statement nesting:
If condition expression 1 Then
If condition expression 2 Then
Statement Block 2-1
Else
Statement Block 2-2
End If
Else
Statement Block 2
End If
For example:
If x> 0 Then
If x> 10 Then
Response. Write (x & "greater than 10 ")
Else
Response. Write (x & "is a positive number smaller than 10 ")
End If
Else
Response. Write (x & "is negative ")
End If
In program design, conditions are often judged one by one, that is, condition 1 is first judged, condition 2 is then judged if condition 1 is not true, and so on. In this case, the following statements can be used.
If... ElseIf statement:
If condition expression 1 Then
Statement Block 1
ElseIf conditional expression 2 Then
Statement Block 2
......
ElseIf conditional expression n Then
Statement Block n
Else
Statement Block n + 1
End If
The execution process of the nested If statement is 3-4. Conditional expression 1 is calculated first. If the value of this conditional expression is "True", Statement 1 is executed. If it is not "True", it is "False ", calculate the value of the next conditional expression (condition expression 2), and so on. If the value of all conditional expressions is not "True ", the statement Block n + 1 under the "Else" clause is executed.

 

Figure 3-4 nested structure of If Statements

Example 3.2: write a program to determine whether the input year is a leap year. The condition for a leap year is:
1) The year that can be divisible by four, but cannot be divisible by 100 is a leap year;
2) The year that can be divisible by 100 and 400 is a leap year.
When running the program, the page shown in 3-5 is displayed. when you enter a year value and press the "Submit" button, the system returns the result of determining whether the year is a leap year, as shown in 3-6.

 

Figure 3-5 Input page

 

Figure 3-6 result

Source code is as follows Ex3-2.aspx ):

Enter a year to help you determine whether a year is leap.

The program partially defines the BtnExe_Click process. Its main function is to judge whether the year value entered by the user in the input box is correct. If the year value is incorrect, an error message is displayed. If the year value is correct, then, the algorithm is used to determine whether the year is a leap year and the label control LblCaution is used to return the result. If statement Nesting is used in the design. Note that the Else clause always works with the unpaired If clause closest to it.

2. Select... Case statement
Select... The Case statement can be used to determine multiple branches of a result. It can be used to select a condition among multiple possibilities, rather than using a large number of nested If... Else... If... Else... If structure.
Syntax:
Select Case variable or expression
Case value 1
Statement Block 1
Case value 2
Statement Block 2
......
Case value n
Statement Block n
[Case Else
Statement Block n + 1]
End Select
Select... The execution process of the Case statement is 3-7.
Here, "value I" is of the same type as "variable or expression". It can be a value, a string, or a value related to other situations to be tested. When the value of a variable or expression matches value 1, execute statement Block 1. When the value matches value 2, execute statement Block 2. When the value n matches, execute statement Block n. If all values do not match, execute statement Block n + 1.
Note: "Case" can contain several values, which can be separated by commas (,), such as 1, 2, and 6. It can also be a data range, such as 1 to 10.
Example 3.3: procedures for changing scores to grades: the integer between 0 and ~ 100) output the corresponding level, rule: 90 ~ 100: Excellent; 80 ~ 89 is good; 70 ~ 79 is classified into medium, 60 ~ 69 points to pass; 0 ~ 59 points fail. If the input score is not 0 ~ If the value is between 100 and, an error is returned.

 

Figure 3-7 Select... Case statement execution process

When running the program, the page shown in 3-8 is displayed first. When you enter a score value and press the "Submit" button, the corresponding grade of the score is returned, as shown in 3-9.

 

Figure 3-8 Score Input page

 

Figure 3-9 returned level results

Source code is as follows Ex3-3.aspx ):

Enter the score

In some programs, convert the text on the TxtGrade control to a value, and then judge in sequence. If the value is between "90 to 100", then execute LblCaution. text = "excellent"; otherwise, continue to judge whether the value is between "80 to 89". If so, execute LblCaution. text = "good"; so on. If no condition is met, the code after Case Else is executed.


BibliographyPrevious sectionNext section

Related Article

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.