Pemrograman Komputer: Semester Genap 20015 /2016

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 44

1

Pemrograman Komputer
Semester Genap 20015 /2016

Programming Langguage

7. Visual Basic
2

PART IV LOOPING

Programming Langguage

Looping
3

It's now time to learn how to write programs that perform

repetitive data processing.


When a computer does something over and over, the
computer is said to be looping.
Loops enable you to correct user errors and repeat certain
program functions when the user requests a repeat.
The repeating statements (loops):
The Do While Loop
The Do Until Loop
The Other Do Loops
The For Loop

Programming Langguage

The Do While Loop


4

The Do statement supports several different loop formats .


The Do While loop is perhaps the most common looping

statement that you'll put in Visual Basic programs.


The Do While statement works with relational expressions
just as the If statement does.
Therefore, the six relational operators work as expected here.
Rather than control the one-time execution of a single block
of code, however, the relational expression controls the
looping statements.
The code that you've seen so far inside event procedures has
been sequential code that executed one statement following
another in the order that you typed the statements. Looping
changes things a bit.
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

The Do While Loop


7

Many lines of your programs will still execute sequentially,

but a loop will cause blocks of code to repeat one or more


times.
Here is the format of the Do While loop:
Do While (relational test)
Block of one or more Visual Basic statements
Loop

The block of code continues looping as long as the relational

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

The Do While Loop


8

The block of code keeps repeating as long as the Do While

loop's relational test continues to stay true.


Eventually, the relational test must become false or your
program will enter an infinite loop and the user will have to
break the program's execution through an inelegant means,
such as pressing the Ctrl+Break key combination.
An infinite loop repeats forever.
Even if you provide an Exit command button, the program

will often ignore the user's Exit command button click if the
program enters an infinite loop.

Programming Langguage

The Do While Loop


9

For example, this Do loop consists of

statements that process input until the


user enters the word Done:
Private Sub Command1_Click()
Dim InpName As String
Do While InpName <> "Done"
InpName = InputBox("Type a name or Done to quit.")
If InpName <> "Done" Then
Label1.Caption = InpName
End If
Loop
End Sub

Programming Langguage

The Do While Loop


10

The conditional statement in this loop is InpName <>

"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

The Do While Loop


11
Private Sub Command1_Click()
Dim StrAge As String
Dim Age As Integer
StrAge = InputBox("How old are you?", "Age Ask")
If (StrAge = "") Then
End
End If
Age = Val(StrAge)
Do While ((Age < 10) Or (Age > 99))
Beep
MsgBox "Your age must be between 10-99", MB_ICONEXCLAMATION, "Error!"
StrAge = InputBox("How old are you?", "Age Ask")
If (StrAge = "") Then
End
End If
Age = Val(StrAge)
Loop
End Sub
Programming Langguage

The Do Until Loop


12

Whereas the Do While loop continues executing the body of

the loop as long as the relational test is true, the Do Until


loop executes the body of the loop as long as the relational
test is false.
The Do Until loop works almost exactly like the Do While
except that the Do Until loop continues executing the body of
the loop until the relational test is true.
Like the Do While, the Do Until is a multiline looping
statement that can execute a block of code that's one or more
lines long.
Here is the format of the Do Until:
Do Until (relational test)
Block of one or more Visual Basic statements
Loop
Programming Langguage

The Do Until Loop


13

The Do Until loop continues executing a block of Visual Basic

statements as long as a relational test is false. As soon as the


relational test becomes true (the loop is said to Do a loop
until the condition becomes false), the loop terminates and
the program continues on the line that follows the closing
Loop statement.

Programming Langguage

Private Sub Form_Activate()


Do
Counter = Counter + 1
Print Counter
Loop Until Counter = 10
End Sub

Programming Langguage

14

Private Sub Form_Activate()


Do Until Counter = 10
Counter = Counter + 1
Print Counter
Loop
End Sub

Programming Langguage

15

Dim n, sum As Integer


Private Sub Form_Activate()
List1.AddItem "n" & vbTab & "sum"
Do Until n = 100
n=n+1
sum = sum + n
List1.AddItem n & vbTab & sum
Loop
End Sub
Private Sub Form_Load ()
n=0
sum = 0
End Sub
Programming Langguage

16

Programming Langguage

17

The Do Until Loop


18

For example, this Do loop consists of

statements that process input until the


user enters the word Done:
Private Sub Command1_Click()
Dim InpName As String
Do Until InpName = "Done"
InpName = InputBox("Type a name or Done to quit.")
If InpName <> "Done" Then
Label1.Caption = InpName
End If
Loop
End Sub

Programming Langguage

The Do Until Loop


19
Private Sub Command1_Click()
Dim StrAge As String
Dim Age As Integer
StrAge = InputBox("How old are you?", "Age Ask")
If (StrAge = "") Then
End
End If
Age = Val(StrAge)
Do Until((Age >= 10) And (Age <= 99))
Beep
MsgBox "Your age must be between 10-99", MB_ICONEXCLAMATION, "Error!"
StrAge = InputBox("How old are you?", "Age Ask")
If (StrAge = "") Then
End
End If
Age = Val(StrAge)
Loop
End Sub
Programming Langguage

The Other Do Loops


20

There is another pair of Do loops that works almost exactly

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

The Other Do Loops


21

Here is the format of the Do-Loop While:


Do
Block of one or more Visual Basic statements
Loop While(relational test)
Here is the format of the Do-Loop Until:
Do
Block of one or more Visual Basic statements
Loop Until (relational test)

Programming Langguage

The Other Do Loops


22

Private Sub Command1_Click()


Dim InpName As String
Do
InpName = InputBox("Type a name or Done to quit.")
If InpName <> "Done" Then
Label1.Caption = InpName
End If
Loop While InpName <> "Done"
End Sub

Programming Langguage

The Other Do Loops


23

Private Sub Command1_Click()


Dim InpName As String
Do
InpName = InputBox("Type a name or Done to quit.")
If InpName <> "Done" Then
Label1.Caption = InpName
End If
Loop Until InpName = "Done"
End Sub

Programming Langguage

The Other Do Loops


24
Private Sub Command1_Click()
Dim StrAge As String
Dim Age As Integer
Do
StrAge = InputBox("How old are you?", "Age Ask")
If (StrAge = "") Then
End
End If
Age = Val(StrAge)
If ((Age < 10) Or (Age > 99)) Then
Beep
MsgBox "Your age must be between 10 and 99", MB_ICONEXCLAMATION,
"Error!"
End If
Loop While ((Age < 10) Or (Age > 99))
End Sub

Programming Langguage

The Other Do Loops


25
Private Sub Command1_Click()
Dim StrAge As String
Dim Age As Integer
Do
StrAge = InputBox("How old are you?", "Age Ask")
If (StrAge = "") Then
End
End If
Age = Val(StrAge)
If ((Age < 10) Or (Age > 99)) Then
Beep
MsgBox "Your age must be between 10 and 99", MB_ICONEXCLAMATION,
"Error!"
End If
Loop Until ((Age >= 10) And (Age <= 99))
End Sub

Programming Langguage

The For Loop


26

The For loop (sometimes called the For-Next loop) also

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)

ONE OR MORE VISUAL BASIC STATEMENTS


NEXT COUNTER

Programming Langguage

ForNext Statement
For I = 0 To 100 Step 2
statement
statement
Next I
Programming Langguage

28

Flow-Chart loop : For


for loop
Statement ;
.
.
end

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.

Private Sub Form_Activate()


For counter = 1 To 10
Print Counter
Next counter
End Sub

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

The For Loop


35

Add the numbers from 1 to 10.


Private Sub Command1_Click()
Dim Sum As Integer
Dim Number As Integer
Sum = 0
For Number = 1 To 10
Sum = Sum + Number
Next Number
Label1.Caption = Sum
End Sub

Programming Langguage

The For Loop


36

The loop counts up from 1 to 20 by 4s, putting each count

into the variable named c and printing a message box each


time. The Step value changes how Visual Basic updates the
CounterVar each time that the loop iterates.
Private Sub Command1_Click()
Dim c As Integer
For c = 1 To 20 Step 4
MsgBox "This is a message box"
Next c
End Sub

Programming Langguage

The For Loop


37

If you specify a negative Step value, Visual Basic counts

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

The For Loop


38

Use the Print method in a ForNext loop to display output on

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 (,)

Places the elements one tab field apart

semicolon (;)

Places elements side by side

Programming Langguage

The For Loop


39

The example below shows how a ForNext loop can change

the text size on a form by changing the form's FontSize


property:
Private Sub Command1_Click()
Dim I As Integer
For I = I To 10
Print "Line"; I
FontSize = 10 + I
Next I
End Sub

Programming Langguage

Exit For Statement


40

In Visual Basic, you can use an Exit For statement to exit a

For...Next loop before the loop has finished executing.


With this capability, you can respond to specific events that
occur before the loop runs the pres et number of times.
For example, the loop prompts the user for print names on the
form, unless the user enters the word Done.
If the user does enter Done, the program jumps to the first
statement that follows the Next statement.

Programming Langguage

Exit For Statement


41

Private Sub Command1_Click()


Dim I As Integer
For I = I To 10
InpName = InputBox("Type a name or Done to quit.")
If InpName = "Done" Then Exit For
Print InpName
Next I
End Sub

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

Private Sub Form_Activate ( )


For firstCounter= 1to 5
Print Hello
For secondCounter=1 to 4
Print Welcome to the VISUAL BASIC tutorial
Next secondCounter
Next firstCounter
Print Thank you
End Sub

Programming Langguage

43

Programming Langguage

44

You might also like