Sei sulla pagina 1di 5

1/25/2017 While...

EndWhileStatement(VisualBasic)

While...End While Statement Visual Basic


Visual Studio 2015

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Runs a series of statements as long as a given condition is True.

Syntax

Whilecondition
[statements]
[ContinueWhile]
[statements]
[ExitWhile]
[statements]
EndWhile

Parts

Term Definition

condition Required. Boolean expression. If condition is Nothing, Visual Basic treats it as False.

https://msdn.microsoft.com/enus/library/zh1f56zs.aspx 1/5
1/25/2017 While...EndWhileStatement(VisualBasic)

statements Optional. One or more statements following While, which run every time condition is True.

ContinueWhile Optional. Transfers control to the next iteration of the While block.

ExitWhile Optional. Transfers control out of the While block.

EndWhile Required. Terminates the definition of the While block.

Remarks
Use a While...EndWhile structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True. If you want more
flexibility with where you test the condition or what result you test it for, you might prefer the Do...Loop Statement. If you want to repeat the statements a set number of
times, the For...Next Statement is usually a better choice.

Note

The While keyword is also used in the Do...Loop Statement, the Skip While Clause and the Take While Clause.

If condition is True, all of the statements run until the EndWhile statement is encountered. Control then returns to the While statement, and condition is again
checked. If condition is still True, the process is repeated. If its False, control passes to the statement that follows the EndWhile statement.

The While statement always checks the condition before it starts the loop. Looping continues while the condition remains True. If condition is False when you first
enter the loop, it doesnt run even once.

The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean Data Type value True or False. This expression
can include a value of another data type, such as a numeric type, that has been converted to Boolean.

You can nest While loops by placing one loop within another. You can also nest different kinds of control structures within one another. For more information, see Nested
Control Structures.

Exit While
https://msdn.microsoft.com/enus/library/zh1f56zs.aspx 2/5
1/25/2017 While...EndWhileStatement(VisualBasic)

The Exit While statement can provide another way to exit a While loop. ExitWhile immediately transfers control to the statement that follows the EndWhile statement.

You typically use ExitWhile after some condition is evaluated for example, in an If...Then...Else structure. You might want to exit a loop if you detect a condition
that makes it unnecessary or impossible to continue iterating, such as an erroneous value or a termination request. You can use ExitWhile when you test for a condition
that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. You can then use ExitWhile to escape the loop.

You can place any number of ExitWhile statements anywhere in the While loop.

When used within nested While loops, ExitWhile transfers control out of the innermost loop and into the next higher level of nesting.

The ContinueWhile statement immediately transfers control to the next iteration of the loop. For more information, see Continue Statement.

Example
In the following example, the statements in the loop continue to run until the index variable is greater than 10.

VB

DimindexAsInteger=0
Whileindex<=10
Debug.Write(index.ToString&"")
index+=1
EndWhile

Debug.WriteLine("")
'Output:012345678910

Example
The following example illustrates the use of the ContinueWhile and ExitWhile statements.

VB

https://msdn.microsoft.com/enus/library/zh1f56zs.aspx 3/5
1/25/2017 While...EndWhileStatement(VisualBasic)

DimindexAsInteger=0
Whileindex<100000
index+=1

'Ifindexisbetween5and7,continue
'withthenextiteration.
Ifindex>=5Andindex<=8Then
ContinueWhile
EndIf

'Displaytheindex.
Debug.Write(index.ToString&"")

'Ifindexis10,exittheloop.
Ifindex=10Then
ExitWhile
EndIf
EndWhile

Debug.WriteLine("")
'Output:1234910

Example
The following example reads all lines in a text file. The OpenText method opens the file and returns a StreamReader that reads the characters. In the While condition, the
Peek method of the StreamReader determines whether the file contains additional characters.

VB

PrivateSubShowText(ByValtextFilePathAsString)
IfSystem.IO.File.Exists(textFilePath)=FalseThen
Debug.WriteLine("FileNotFound:"&textFilePath)
Else
DimsrAsSystem.IO.StreamReader=System.IO.File.OpenText(textFilePath)

Whilesr.Peek()>=0
Debug.WriteLine(sr.ReadLine())
https://msdn.microsoft.com/enus/library/zh1f56zs.aspx 4/5
1/25/2017 While...EndWhileStatement(VisualBasic)

EndWhile

sr.Close()
EndIf
EndSub

See Also
Loop Structures
Do...Loop Statement
For...Next Statement
Boolean Data Type
Nested Control Structures
Exit Statement
Continue Statement

2017 Microsoft

https://msdn.microsoft.com/enus/library/zh1f56zs.aspx 5/5

Potrebbero piacerti anche