Overview of VB Control structure

Source: Internet
Author: User
Tags exit case statement integer numeric numeric value

Loop structure
The loop structure allows you to repeat a line or lines of code. The looping constructs supported by Visual Basic are:
1.Do ... Loop
2.For ... Next
3.For Each ... Next

Do ... Loop
Repeats a block of statements with the Do loop, and the number of repetitions is variable. Do ... The Loop statement has several evolutionary forms, but each evaluates to a numeric condition to determine whether to continue execution. As If ... Thencondition must be a numeric value or an expression that evaluates to True (not 0) or False (0). In the following do ... In the loop loop, the statements is executed as long as condition is true.
Do While condition
Statements

Loop
When Visual Basic performs this do loop, it first tests the condition. If condition is False (0), all statements are skipped. If condition is true (not 0), VisualBasic executes the statement and then returns to the do-while statement to test the condition.
Therefore, as long as the condition is true or Non-zero, the loop can be executed as many times as it pleases. If condition is false at the beginning, the statement is not executed. For example, the following procedure calculates the number of occurrences of a target string in another string, and executes the loop as soon as it discovers the target strings:
Function countstrings (longstring, Target)
Dim position, Count
Position = 1
Do While InStr (position, longstring, target)
Position = InStr (position, longstring, Target) _
+ 1
Count = Count + 1
Loop
Countstrings = Count
End Function
If the destination string does not appear in another string, InStr returns 0, and the loop is no longer executed.
Do ... Another evolutionary form of the Loop statement is to execute the statement first, and then test the condition after each execution. This form guarantees that the statements is executed at least once:
Todo
Statements
Loop while condition
The other two evolution forms are similar to the first two, and the difference is that they perform loops as long as condition is false instead of true.

For ..... Next000
Do loops are appropriate when you do not know how many statements you need to execute within a loop. However, when you know how many times you want to execute, it's best to use for ... Next Loop. Unlike do loops, A for loop uses a variable called a counter that, after each iteration, increases or decreases the value of the counter variable. The syntax for the For loop is as follows:
For counter = "start to" [Step increment]
Statements
Next [counter]
Parameter Counter, Start, end, and increment are all numeric.
Note that the increment parameter can be positive and negative. If increment is positive, the Start must be less than or equal to end, or the statement within the loop cannot be executed. If increment is negative, Start must be greater than or equal to end so that the loop body can be executed. If you do not set step, the increment default value is 1.
When you perform a For loop, Visual Basic
1. Set counter equals start.
2. Test whether the counter is greater than end. If so, Visual Basic exits the loop. (if increment is negative, Visual Basic tests whether the counter is less than end.) )
3. Execute the statement.
4. Counter add one, or add increment (if specified).
5. Repeat steps 2 through 4.
The following code prints out all valid screen font names:
Private Sub Form_Click ()
Dim I as Integer
For i = 0 to Screen.fontcount
Print screen.fonts (i)
Next
End Sub
In the VCR sample application, the Highlightbutton procedure uses the for ... Next loop, step through the collection of controls on the VCR form and display the appropriate Shape controls:
Sub Highlightbutton (mycontrol as Variant)
Dim I as Integer
For i = 0 to Frmvcr.controls.count-1
If TypeOf Frmvcr.controls (i) is Shape Then
If Frmvcr.controls (i). Name = MyControl Then
Frmvcr.controls (i). Visible = True
Else
Frmvcr.controls (i). Visible = False
End If
End If
Next
End Sub

For Each ... Next
For Each ... Next Loop with for ... The Next loop is similar, but it repeats a set of statements for each element in an array or collection of objects, rather than a certain number of repeated statements. If you don't know how many elements a collection has, for each ... The Next loop is useful.
For Each ... The syntax for the Next loop is as follows:
for every element in group
Statements
Next Elementt
For example, the following child procedure opens the Biblio.mdb, adding the name of each table to the list box.
Sub listtabledefs ()
Dim objdb as Database
Dim mytabledef As TableDef
Set objdb = OpenDatabase ("C:\vb\biblio . mdb, _
True, False)
for each mytabledef in Objdb.tabledefs ()
List1.AddItem mytabledef.name
Next Mytabledef
End Sub
, remember to use for each ... Next:
1. For a collection, an element can only be a Variant variable, or a general object variable, or an object that is listed in the Object Browser.
2. An array, an element can only be a variant variable.
3.For Each ... Next cannot be used with an array of user-defined types because a Variant cannot contain a user-defined type.

Using Control structures
Nested control structures can place control structures within another control structure (for example, for ...). The If ... in the Next loop. Then block). One control structure contains another control structure called Nest (nested). In Visual Basic, there is no limit to the number of nested layers of the control structure. According to the general custom, in order to make the judgment structure and the circulation structure more readable, it always uses the indented way to write the body part of the judgment structure or circulation.
For example, the following procedure prints all the font names that are common to the printer and screen:
Private Sub Form_Click ()
Dim Sfont, Pfont
For each sfont in Screen.fonts ()
For each pfont in Printer.fonts ()
If Sfont = Pfont Then
Print Sfont
End If
Next Pfont
Next Sfont
End Sub
Note that the first Next closes the inner for Loop, and the last for closes the outer for loop. Similarly, in nested IF statements, the end If statement is automatically paired with the nearest previous if statement. Nested do ... The loop structure works the same way, and the loop statement with the most inner loop matches the do statement in the inner circle.

Exit control Structure
With the exit statement, you can exit the For loop, do loop, child procedure, or Function procedure directly. The syntax for the exit statement is simple: Exit for has no limit on the number of occurrences in the For loop, and the number of times the exit do appears in the Do Loop is unlimited.
For counter = start to end
[Step increment]
[Statementblock]
[Exit for]
[Statementblock]
Next [counter[, Counter] [,...]]
Do [{while | Until} condition]
[Statementblock]
[Exit do]
[Statementblock]

The

Loop
Exit do statement can be used in all versions of the Do loop syntax. The
Exit for and exit Do is useful because it is sometimes appropriate to exit the loop immediately and no longer executes any further iterations or statements in the loop. For example, in the previous example of a print screen and a common font for a printer, the program continually compares the printer font to a given screen font, even after it has found a matching typewriter font. There is a more efficient version of this function, where you exit the loop as soon as you find the matching font:
Private Sub form_click ()
Dim Sfont, Pfont
for every sfont in Screen.fonts ()
for each pfont in printer.fonts ()
If sfont = Pfont Then
Print sfont
Exit for ' exits the inner circle loop.
End If
Next pfont
Next sfont
End Sub
As this example shows, the Exit statement almost always appears inside an if statement or a Select Case statement, while an if statement or a Select Case Statements are nested within loops.
When you break a loop with the exit statement, the value of the counter variable differs by the way it exits the loop:
1. When the loop is complete, the counter value equals the upper bound value plus the stepping value.
2. When you exit the loop early, the counter variable retains its value and complies with the general rules for the range of values.
3. If the counter variable is of type Object, the value is nothing, and if the counter variable is a Variant type, the value is Empty.

To exit a child procedure or Function procedure
You can also exit the process from within the control structure. The syntax for the exit Sub and exit Function is similar to the exit for and exit Do in the previous section, "Exit control structure." The Exit Sub can appear anywhere within the body of the child process, and the number of occurrences varies depending on the need.
The exit Sub and the exit Function are useful when the process has completed each task and can be returned directly. For example, if you want to change the previous example so that you print only the first of the common fonts for the printer and screen you find, you can use the Exit Sub:
Private Sub Form_Click ()
Dim Sfont, Pfont
For each sfont in Screen.fonts ()
For each pfont in Printer.fonts ()
If Sfont = Pfont Then
Print Sfont
Exit Sub ' exits the procedure.
End If
Next Pfont
Next Sfont
End Sub

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.