A loop is a structure used to repeat some operations. Loop control is required for many practical problems, such as the sum of multiple numbers, input or output of multiple values. VB. NET has four statements For loop implementation: 1)... Next; 2) For Each... Next; 3) Do... Loop; 4) While .]
1. For... Next statement
Syntax:
For Loop Variable = initial value to final value [Step size]
Statement Block
[Exit For]
[Statement block]
Next [cyclic variable]
For... the Next loop statement is mainly used to predict the number of loops.
The loop variable in the syntax must be numeric to control the number of times the loop is executed. If the step size is positive, the initial value must be smaller than the end value. If the step size is negative, the initial value is greater than the end value. The default value is 1. The function of Exit For is to terminate the loop. When this statement is run, the loop is exited and the statement after Next is executed.
The execution process of the For... Next statement is shown in 3-10.
|
Figure 3-10 For... Next flow chart |
For example, the following program section calculates 1 ~ The sum of all natural numbers between 100.
Dim Sum As Integer = 0 Dim i As Interger For i = 1 To 100 Sum += i Next |
<% @ Page Language = "vb" %>
Where I is a loop variable, used as the loop counter in the For... Next loop.
Example 3.4: design the ASP. NET page shown in 3-11 ~ The number 7 is continuously changed, and the row number and "Hello!" are displayed !" Text.
|
Figure 3-11 use a loop to implement 1-11 in the table ~ Display of continuous changes in the word 7 |
Source code is as follows Ex3-4.aspx ):
<% @ Page Language = "vb" %>
<%Dim IDim FontStr1, FontStr2For I = 1 To 7FontStr1 = ""FontStr2 = ""Response. Write ("
"& FontStr1) Response. Write (I & FontStr2 &" | ")Response. Write ("
"& FontStr1) Response. Write ("Hello! "& FontStr1 &" | ")Response. Write ("
")Next%>
|
<% @ Page Language = "vb" %> In example 3.4, the text size displayed in the table is controlled through the For loop, and the display font size is controlled by the loop variable I. When I is 1, the font Size value is 1. When I is 2, the font Size value is 2.
ASP. NET built-in object Response is used in the program, which is mainly used to send messages to the browser. In this example, the Write method is used to return the displayed message to the browser. Chapter 1 describes ASP. NET built-in objects and their usage.
2. For Each... Next statement
This statement is specially designed For collections and arrays. It is very similar in format to the For... Next statement.
Syntax:
For Each expression In group
Statement Block
[Exit For]
[Statement block]
Next
If the expression is a collection or array element, the statement block operation is repeated. The meaning of Exit For is the same as that before.
For example:
Dim Score () As Integer = {80, 90, 89, 98} 'defines the Integer array Score and assigns the Initial Value Dim mark As Integer, Sum As Integer = 0 For mark In Score ', calculate the sum of each element In the array Score (). Sum + = mark Next |
The above code calculates the sum of the elements in the Score () array. When the first cycle is executed, the mark is Score (0), and the subsequent cycles are: Score (1), Score (2 )..., when the last element in Score () is processed, the loop ends. For an introduction to arrays, see section 3.5.
3. Do... loop statement
The function is to repeatedly execute a set of statements until a condition is "True" to terminate the loop. It is used to control the loop structure with unknown number of loops. The Do... Loop statement has two formats.
Syntax:
Do {While/Until} condition expression
Statement Block
[Exit Do]
Statement Block
Loop 'do... Loop statement format 1
Or:
Do
Statement Block
[Exit Do]
Statement Block
Loop {While/Until} condition expression 'do... Loop statement format 2
The first is to judge the condition first, and then execute the loop body. The loop may not be executed once. The second is to execute the loop body first and then judge the condition. The loop must be executed at least once.
When the Exit Do statement is run, Exit the Loop and execute the next statement of the Loop.
The execution process of Do... Loop statements is shown in 3-12.
|
Figure 3-12 Do... Loop structure Flowchart |
Example 3.5: Guess the number of games. As shown in figure 3-13 of the program interface, the user can guess if a number is set to 108 in this example. In the text box, enter a number and click "guess right ?" Button. If you are guessing, a success prompt is displayed. If you have guessed the error, a prompt "greater than" or "less than" is also displayed.
Source code is as follows Ex3-5.aspx ):
The Btn_Click process in the program uses Do While... Loop. When the number of inputs is not equal to the set number, the Response. Redirect operation is executed repeatedly. The function of this operation is to Redirect the page. In this example, the target of the redirect page still points to the Ex3-5.aspx of this file), that is, if you have not guessed it again.
In this example, another process Page_Load () is designed, which is executed when the page is loaded in the browser. In this process, Request ("hint") is used to read the passed hint parameter value. Request is another important object of ASP. NET, which is opposite to Response and reads data from a browser. In addition, this example uses the technology of passing parameters between pages. You can use parameters when you want to transfer parameters from one page to another. In this example, Response. Redirect ("Ex3-5.aspx? Hint = "&" is too small! ") The operation is to achieve when you turn to the Ex3-5.aspx page, to the page to input the name hint, the value is" too small !" The value can be read through the Request object. The content of ASP. NET objects and parameter passing will be detailed in chapter 5th. This chapter and the next chapter will also be used for examples.
You can change the Do while... Loop in example 3.3 to Do... Loop Until Loop format. Please modify it by yourself.
4. While loop statement
The While loop is similar to the Do While loop. It also determines the condition first and then executes the loop body.
Syntax:
While condition expression
Statement Block
[Exit While]
[Statement block]
End While
Exit While is the exclusive body.
BibliographyPrevious sectionNext section |