Condividi tramite


Continue Statement (Visual Basic)

Transfers control immediately to the next iteration of a loop.

Continue { Do | For | While }

Remarks

You can transfer from inside a Do, For, or While loop to the next iteration of that loop. Control passes immediately to the loop condition test, which is equivalent to transferring to the For or While statement, or to the Do or Loop statement that contains the Until or While clause.

You can use Continue at any location in the loop that allows transfers. The rules allowing transfer of control are the same as with the GoTo Statement.

For example, if a loop is totally contained within a Try block, a Catch block, or a Finally block, you can use Continue to transfer out of the loop. If, on the other hand, the Try...End Try structure is contained within the loop, you cannot use Continue to transfer control out of the Finally block, and you can use it to transfer out of a Try or Catch block only if you transfer completely out of the Try...End Try structure.

If you have nested loops of the same type, for example a Do loop within another Do loop, a Continue Do statement skips to the next iteration of the innermost Do loop that contains it. You cannot use Continue to skip to the next iteration of a containing loop of the same type.

If you have nested loops of different types, for example a Do loop within a For loop, you can skip to the next iteration of either loop by using either Continue Do or Continue For.

Example

The following code example uses the Continue While statement to skip to the next column of an array if a divisor is zero. The Continue While is inside a For loop. It transfers to the While col < lastcol statement, which is the next iteration of the innermost While loop that contains the For loop.

Dim row, col As Integer 
Dim lastrow As Integer = 6
Dim lastcol As Integer = 10
Dim a(,) As Double = New Double(lastrow, lastcol) {}
Dim b(7) As Double
row = -1
While row < lastrow
    row += 1
    col = -1
    While col < lastcol
        col += 1
        a(row, col) = 0
        For i As Integer = 0 To b.GetUpperBound(0)
            If b(i) = col Then 
                Continue While 
            Else
                a(row, col) += (row + b(i)) / (col - b(i))
            End If 
        Next i
    End While 
End While

See Also

Reference

Do...Loop Statement (Visual Basic)

For...Next Statement (Visual Basic)

While...End While Statement (Visual Basic)

Try...Catch...Finally Statement (Visual Basic)