Вы находитесь на странице: 1из 4

Se rcomienda usar Visual Studio 2017

Instrucción While...End While (Visual


Basic)
Ejecuta una serie de instrucciones siempre que una condición dada sea True.
While condition
[ statements ]
[ Continue While ]
[ statements ]
[ Exit While ]
[ statements ]
End While

Elementos
Término Definición

condition Requerido. Expresión Boolean. Si condition es Nothing, Visual Basic la


trata como False.

statements Opcional. Una o más instrucciones a continuación de While, que se


ejecutan cada vez que condition es True.

Continue Opcional. Transfiere el control a la siguiente iteración del bloque de While .


While

Exit While Opcional. Transfiere el control fuera del bloque While.

End While Requerido. Termina la definición del bloque While.

Ejemplo
En el ejemplo siguiente, las instrucciones del bucle continúan ejecutándose hasta que la
variable index es superior a 10.
VB
Dim index As Integer = 0
While index <= 10
Debug.Write(index.ToString & " ")
index += 1
End While

Debug.WriteLine("")
' Output: 0 1 2 3 4 5 6 7 8 9 10

En el siguiente ejemplo se muestra el uso de las instrucciones Continue While y Exit While.
VB
Dim index As Integer = 0
While index < 100000
index += 1

' If index is between 5 and 7, continue


' with the next iteration.
If index >= 5 And index <= 8 Then
Continue While
End If

' Display the index.


Debug.Write(index.ToString & " ")

' If index is 10, exit the loop.


If index = 10 Then
Exit While
End If
End While

Debug.WriteLine("")
' Output: 1 2 3 4 9 10

En el ejemplo siguiente se leen todas las líneas de un archivo de texto. El método OpenText abre el
archivo y devuelve un StreamReader que lee los caracteres. En la condición de While , el método
de Peek de StreamReader determina si contiene caracteres adicionales.
For

Repeats a group of statements a specified number of times.+


Syntax
Copy

For counter [ As datatype ] = start To end [ Step step ]


[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]

Parts
Part Description

counter Required in the For statement. Numeric variable. The control variable for the loop. For more
information, see Counter Argument later in this topic.

datatype Optional. Data type of counter . For more information, see Counter Argument later in this topic.

start Required. Numeric expression. The initial value of counter .

end Required. Numeric expression. The final value of counter .

step Optional. Numeric expression. The amount by which counter is incremented each time through
the loop.

statements Optional. One or more statements between For and Next that run the specified number of times.

Continue Optional. Transfers control to the next loop iteration.


For

Exit For Optional. Transfers control out of the For loop.

Next Required. Terminates the definition of the For loop.


Simple Examples
You use a For ... Next structure when you want to repeat a set of statements a set
number of times.

In the following example, the index variable starts with a value of 1 and is
incremented with each iteration of the loop, ending after the value
of index reaches 5.
VBCopy

For index As Integer = 1 To 5


Debug.Write(index.ToString & " ")
Next
Debug.WriteLine("")
' Output: 1 2 3 4 5

In the following example, the number variable starts at 2 and is reduced by 0.25 on
each iteration of the loop, ending after the value of number reaches 0.
The Step argument of -.25 reduces the value by 0.25 on each iteration of the
loop.
VBCopy

For number As Double = 2 To 0 Step -0.25


Debug.Write(number.ToString & " ")
Next
Debug.WriteLine("")
' Output: 2 1.75 1.5 1.25 1 0.75 0.5 0.25 0

Вам также может понравиться