Current location - Training Enrollment Network - Mathematics courses - The DoLoop statement in mathematics taught me how to do it. Urgent! !
The DoLoop statement in mathematics taught me how to do it. Urgent! !
The loop structures supported by Visual Basic are:

Make a ... ring

For ... and then

For each ... and then

Make a ... ring

Use Do loop to execute a statement block repeatedly, and the number of repetitions is uncertain. There are several evolutionary forms of making ... loop statements, but each one evaluates numerical conditions to decide whether to continue execution. Just like if ... then the condition must be a numerical value or an expression whose value is true (non-zero) or false (zero).

What to do below ... Loop loop, as long as the condition is true, execute the statement.

Do While condition

statement

ring

When Visual Basic executes this Do loop, it will first test the condition. If the condition is false (zero), all statements are skipped. If the condition is true (non-zero), Visual Basic will execute the statement and then return 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 needed. If the condition is false from the beginning, the statement will not be executed. For example, the following procedure will count the number of occurrences of one target string in another string and execute a loop when the target string is found:

Function CountStrings (longstring, target)

Dim position, counting

Location = 1

Do While InStr (location, long string, target)

position = InStr(position,longstring,target)_

+ 1

Count = Count+1

ring

CountStrings = count

End function

If the target string does not appear in another string, InStr returns 0 and the loop is no longer executed.

Another evolution of Do ... loop statement is to execute the statement first, and then test the condition after each execution. This form ensures that the statement is executed at least once:

do

statement

Cyclic While condition

The other two forms of evolution are similar to the first two, except that as long as the condition is false rather than true, they execute a cycle.

Loop at least once, zero times or more.

Do until condition

statement

Cyclic Do

statement

Loop until the condition

. For ... and then

When you don't know how many times a statement needs to be executed in a loop, you should use a do loop. However, when you know how many times to execute, it's best to use For ... the next loop. Unlike the Do loop, the For loop uses a variable called a counter, whose value increases or decreases after each iteration of the loop. The syntax of the For loop is as follows:

For Counter = Start to End [Step Increment]

statement

Next [counter]

Parameter counters, start, end and increment are all numbers.

Note that the increment parameter can be positive or negative. If increment is positive, Start must be less than or equal to end, otherwise the statement in the loop cannot be executed. If increment is negative, Start must be greater than or equal to end in order to execute the loop body. If no step size is set, the default value of increment is 1.

When executing a For loop, Visual Basic

Set the counter to start.

Test whether the counter is greater than end. If so, Visual Basic will exit the loop.

(If the increment is negative, Visual Basic will test whether the counter is less than the end. )

Execute the statement.

The counter is incremented by 1, or incremented if specified.

Repeat steps 2 to 4.

The following code prints out all valid screen font names:

Private Sub-Form _Click ()

Mark I as an integer

Filter for i = 0. FontCount

Print the screen. Font (1)

then

End joint

In the VCR sample application, the HighlightButton procedure uses the For ... next loop to traverse the control collection of the VCR form and display the appropriate shape control:

Sub HighlightButton (MyControl as variable)

Mark I as an integer

For i = 0 to frmVCR. Control. Count-1

If the type of frmVCR. Control (I) is shape

If frmVCR. Control (1). Name = MyControl Then

frmVCR。 Control (1). Visible = true

other

frmVCR。 Control (1). Visible = false

If ... it will be over.

If ... it will be over.

then

End joint

For each ... and then

The ... next loop of each is similar to the For ... next loop, but it repeats a set of statements for each element in an array or collection of objects instead of repeating these statements a certain number of times. If you don't know how many elements there are in a collection, the ... next loop of each element is very useful.

The grammar of each one ... The next cycle is as follows:

For each element in the group

statement

The next element t

For example, the following subprocess opens Biblio.mdb and adds the name of each table to the list box.

Sublist Definition ()

Dim objDb as database

Dim MyTableDef to TableDef.

set objDb = open database(" c:\ VB \ biblio . MDB ",_

True or false)

For each MyTableDef in objDb. TableDefs()

List 1。 AddItem MyTableDef。 name

The next MyTableDef

End joint

When using each, please keep in mind some restrictions ... Next:

For collections, elements can only be variables, generic object variables, or objects listed in the object browser.

For arrays, element can only be a Variant variable.

Next cannot be used for arrays of user-defined types for each ... because Variant cannot contain user-defined types.

Visual Basic procedures can test conditional expressions and then perform different operations according to the test results. The decision structures supported by Visual Basic are:

If ... then

If ... then ... other.

Select Case

If ... then

Conditionally execute one or more statements using If ... and then structure. Both single-line syntax and multi-line block syntax can be used:

If conditional Then statement

If the conditions then

statement

If ... it will be over.

The condition is usually a comparison, but it can also be any expression that calculates a numerical value. Visual Basic interprets this value as True or False: zero value is False, and any non-zero value is considered True. If condition is True, Visual Basic will execute all statements after the Then keyword. You can use single-line or multi-line syntax to execute statements conditionally (the following two examples are equivalent):

If any date < now and then any date = now.

If any date < (give advice or help) hello

Any date = now

If ... it will be over.

Note: the single-line format of If ... does not use the End If statement. If you want to execute multi-line code when the condition is true, you must use multi-line blocks, and if ... then ... end the if syntax.

If any date < (give advice or help) hello

Any date = now

Timer 1. "Enabled = False" timer control failed.

If ... it will be over.

If ... then ... other.

Use If ... and then ... Else block to define several statement blocks and execute one of them:

If the condition 1

[Statement Block-1]

so

[Statement Block -2]] ...

[Otherwise.

[statement block -n]]

If ... it will be over.

Visual Basic first tests condition 1. If False, Visual Basic will test condition2, and so on until it finds the True condition. When the condition is found to be true, Visual Basic will execute the corresponding statement block, and then execute the code after End If. As an option, you can include an Else statement block, which Visual Basic will execute if the condition is not true.

If ... then ... Else if is just a special case of if ... then ... otherwise. Note that any number of ElseIf clauses can be used, or none at all. There can be an Else clause whether there is an ElseIf clause or not.

For example, an application can take corresponding actions according to which control in the menu control array is clicked:

Private Sub mnuCut_Click (index is integer)

If index = 0, execute the "cut" command.

"CopyActiveControl" calls the general procedure.

ClearActiveControl

ElseIf Index = 1 Then' copy command.

CopyActiveControl

ElseIf Index = 2, and then execute the "Clear" command.

ClearActiveControl

Else's paste command.

PasteActiveControl

If ... it will be over.

End joint

Note that you can always add more else If blocks to the if ... and then structure. However, when each ElseIf compares the same expression with different values, this structure is boring to write. In this case, you can use Select Case to determine the structure.

For details, please refer to "If ... and then ... Else statement".

Select Case

Visual Basic provides a Select Case structure instead of an If ... and then ... Otherwise, one of them can be selectively executed in multiple statement blocks. The function of the Select Case statement is similar to the If statement ... and then ... Else statement, but in the case of multiple choices, the Select Case statement makes the code more readable.

Select Case processes a test expression structurally and evaluates it only once. Visual Basic then compares the value of the expression with the value of each case in the structure. If they are equal, the statement block associated with the case is executed.

Select case test expression

[List of case expressions 1

[Statement Block-1]]

[Case Expression List 2]

[Statement Block -2]]

.

.

.

[Other circumstances]

[statement block -n]]

End selection

Each expression list is a list of one or more values. If there are multiple values in the list, separate them with commas. Each statement block contains zero or more statements. If multiple cases match the test expression, the associated statement block is executed only for the first matching case. If there is no value matching the test expression in the expression list, Visual Basic will execute the statement in the Case Else clause (this option is optional).

For example, suppose you want to add a command to the edit menu in the example of If ... and then ... otherwise. To do this, you can add another ElseIf clause or write a function with Select Case:

Private Sub mnuCut_Click (index is integer)

Select case index

Case 0' cut command.

"CopyActiveControl" calls the general procedure.

ClearActiveControl

Case 1' copy command.

CopyActiveControl

Case 2' Clear command.

ClearActiveControl

Case 3' Paste command.

PasteActiveControl

Other cases

Show displays the found dialog box.

End selection

End joint

Note that the Select Case structure always evaluates the expression at the beginning. If ... and then ... Else structure evaluates different expressions for each ElseIf statement. Only when the If statement and each ElseIf statement evaluate the same expression, the If ... then ... Else structure will be replaced by the Select Case structure.

Assign the value of an expression to a variable or property.

grammar

[Let] varname = expression

The syntax of the Let statement contains the following parts:

Partial description

Let is optional. Explicit use of the Let keyword is also a format, but it is usually omitted.

Varname is required. The name of the variable or property; Follow the standard variable naming convention.

An expression is required. The value assigned to a variable or property.

explain

Only when the expression is a data type compatible with variables can the value of the expression be assigned to variables or properties. You cannot assign the value of a string expression to a numeric variable, nor can you assign the value of a numeric expression to a string variable. If you do this, you will get an error at compile time.

You can assign a value to a variable with a string or numeric expression, but the reverse may be incorrect. Any Variant except Null can be assigned to string variables, but only when the value of Variant can be interpreted as a number can it be assigned to numeric variables. You can use the IsNumeric function to confirm whether a variable can be converted into a number.

Be careful when assigning an expression of one numeric type to a variable of another numeric type, which will force the value of the expression to be converted to the numeric type of the result variable.

Let statement can assign variables of record type to variables of the same user-defined type. The LSet statement can be used to assign values to record variables of different user-defined types. Use the Set statement to assign an object reference to a variable.