Sunday, December 19, 2010

Do while Loop


The Loop Started with Do While Keyword. While loop to execute a block of code until a condition becomes False.

Syntax:

Do [ While Until ]
Statements
Exit Do
Statements
Loop


The Do While Statement will execute until the condition is False.
The Do Until Statement will execute until the condition is True.

Syntax:


Do
Statements
Exit Do
Statements
Loop [ While Until ]


Example for Do While

Dim a As Integer
a=1
do while a<=6 MsgBox(“ Now A value is” & a) a=a+1 Loop Example for Do Until

Dim a As Integer
a=1
do until a>=6

MsgBox(“ Now A value is” & a)
a=a+1
Loop


The above example Do while Loop will execute over and over until the variable “a” becomes greater than the value 7.
Sometimes, you may want to exit a While loop before the condition is False. We can exit a loop at any time by using the Exit Do Keyword.


Example

Dim a As Integer
a=1
do while a<=6 MsgBox(“ Now A value is” & a) a=a+1 if a=3 then Exit Do End If Loop