Do Loop Until and Do While Loop in Visual Basic Exercise and Example Practice Question
Do While Loop
A loop
is one of the most important structures in VB and is used to repeat a sequence
of statements a number of times.
The Do While
Loop repeats a sequence of statements either as long as or until a certain
condition is true.
Do While condition
statements
Exercise
1. Sub
Command1_Click ( )
Dim num As Integer
Let num=1
Do While num<=10
Picture1.Print
num;
Let
num=num+1
End Sub
2 Sub
Command1_Click ( )
Dim passWord as String, info As String
If Ucase(Text1.Text) = “SECRET.TXT” Then
Let passWord=“”
Do While
passWord <> “SHAHID”
Let
passWord = InputBox$(“Enter passWord”)
Let
passWord = Ucase(passWord)
End If
Open
Text1.Text For Input As #1
Input #1, info
Picture1.Print info
Close #1
End Sub
.
Do Loop Until
A loop
is one of the most important structures in VB and is used to repeat a sequence
of statements a number of times.
The Do Loop
Until executes the statement(s) once and then repeats a sequence of statements either as
long as or until a certain condition is true.
Do
.
.
statements
.
.
Loop Until condition
A
Do Loop of this form executes the statements inside the loop (at least once)
and then checks the truth value of the condition. If the condition is true then
the program continues with the line after the Loop
statement. But if the condition is false then the entire process is repeated
beginning with the Do statement.
Exercise –
Private
Sub Command1_Click()
Dim pw As String, info As String
Do
pw = InputBox$("What is the
password?")
pw = UCase$(pw)
Loop Until pw = "NATASHA"
Open "Secret.txt" For Input As #1
Input #1, info
Picture1.Print info
Close #1
End
Sub
0 comments:
Post a Comment