Professional Documents
Culture Documents
Pemrograman Komputer: Semester Genap 20015 /2016
Pemrograman Komputer: Semester Genap 20015 /2016
Pemrograman Komputer: Semester Genap 20015 /2016
Pemrograman Komputer
Semester Genap 20015 /2016
Programming Langguage
7. Visual Basic
2
PART IV LOOPING
Programming Langguage
Looping
3
Programming Langguage
Start
D0-While
Salah
kondisi
Benar
statement
Exit-Do
Ya
end
Do While I < 5
I=I+1
Loop
Statemen
t
loop
Programming Langguage
Do While condition
.
[Statement]
..
Exit Do
[statement]
..
Loop
Do While
Private Sub Form_Activate
Do while counter <10
counter =counter+1
Print Counter
Loop
End Sub
Programming Langguage
test is true.
Whether you insert one or several lines of code for the block
doesn't matter.
It's vital, however, that the block of code somehow change a
variable used in the relational test.
Programming Langguage
will often ignore the user's Exit command button click if the
program enters an infinite loop.
Programming Langguage
Programming Langguage
"Done".
The Visual Basic compiler translates this statement to mean
loop as long as the InpName variable doesnt contain the
word "Done
This type of loop requires an extra If...Then structure to
prevent the exit value from being displayed when the user
types it.
Programming Langguage
Programming Langguage
Programming Langguage
14
Programming Langguage
15
16
Programming Langguage
17
Programming Langguage
like the two previous sections' loops. Do-Loop While and the
Do-Loop Until look very much like their counterparts that
you learned earlier. Nevertheless, these new loop formats
check their relational tests at the bottom of the loop rather
than at the top.
If a loop begins with a single Do statement, the loop ends
with either Loop While or Loop Until.
If you want the loop to always run at least once in a program,
put the conditional test at the bottom of the loop.
Programming Langguage
Programming Langguage
Programming Langguage
Programming Langguage
Programming Langguage
Programming Langguage
creates a loop.
Unlike the Do loops, however, the For loop repeats for a
specified number of times.
There isn't one correct loop to use in all situations. The For
statement provides the mechanism for the fifth Visual Basic
loop construction that you'll learn. A For loop always begins
with the For statement and ends with the Next statement.
Here is the format of the For loop:
For CounterVar = StartVal To EndVal [Step IncrementVal]
Block of one or more Visual Basic statements
Next CounterVar
Programming Langguage
For
27
FOR COUNTER=STARTNUM TO
ENDNUM (STEP INCREMENT)
Programming Langguage
ForNext Statement
For I = 0 To 100 Step 2
statement
statement
Next I
Programming Langguage
28
Programming Langguage
29
Contoh program
30
Programming Langguage
Example 10.1
This program will generate a column of 10 numbers, starting from 1
and ending at 10. The output is shown in Figure 10.1.
Programming Langguage
31
Programming Langguage
32
Programming Langguage
33
Example 10.2
Private Sub Form_Activate ()
For counter=0 to 100 step 10
Print counter
Next counter
End Sub
Programming Langguage
34
Programming Langguage
Programming Langguage
down.
Private Sub Command1_Click()
Dim c As Integer
For c = 20 To 1 Step -4
MsgBox "This is a message box"
Next c
End Sub
Programming Langguage
a form:
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 10
Print "Line"; i
Next i
End Sub
Symbol
Behavior
comma (,)
semicolon (;)
Programming Langguage
Programming Langguage
Programming Langguage
Programming Langguage
NESTED LOOP
For counter1=startNumber to endNumber (Step increment)
For counter2=startNumber to endNumber (Step increment)
One or more VISUAL BASIC statements
Next counter2
Next counter1
Programming Langguage
42
Programming Langguage
43
Programming Langguage
44