HandsOn Using the DoWhile Loop with a Condition at the Bottom of the Loop

1. In the Visual Basic Editor window, insert a new module and type the SignIn procedure as shown below.

Sub SignIn()

Dim secretCode As String

Do secretCode = InputBox("Enter your secret code:") If secretCode = "sp1045" Then Exit Do Loop While secretCode <> "sp1045" End Sub

Notice that by the time the condition is evaluated, Visual Basic has already executed the statements one time. In addition to placing the condition at the end of the loop, the SignIn procedure shows again how to exit the loop when a condition is reached. When the Exit Do statement is encountered, the loop ends immediately.

To exit the loop in the SignIn procedure without entering the password, you may revise it as follows:

Sub SignIn2()

Dim secretCode As String

Do secretCode = InputBox("Enter your secret code:")

Part I

If secretCode = "sp1045" Or secretCode = "" Then Exit Do Loop While secretCode <> "sp1045" End Sub

0 0

Post a comment