7_-_looping-sep-2015

44
Programming Langguage Semester Genap 20015 /2016 1 Pemrograman Komputer

Upload: ariel-wilka

Post on 05-Dec-2015

240 views

Category:

Documents


7 download

DESCRIPTION

vb

TRANSCRIPT

Page 1: 7_-_Looping-SEP-2015

Programming Langguage

Semester Genap 20015 /2016

1

Pemrograman Komputer

Page 2: 7_-_Looping-SEP-2015

Programming Langguage

PART IV – LOOPING

2

7. Visual Basic

Page 3: 7_-_Looping-SEP-2015

Programming Langguage

Looping3

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 LoopThe Do Until LoopThe Other Do LoopsThe For Loop

Page 4: 7_-_Looping-SEP-2015

Programming Langguage

The Do While Loop4

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.

Page 5: 7_-_Looping-SEP-2015

Programming Langguage 5

kondisi

D0-While

Salah

Benar

statement

Exit-Do Yaend

Statement

loop

Start Do While “condition” …………. [Statement] ………..Exit Do ………… [statement] ………..Loop

Do While I < 5 I = I + 1Loop

Page 6: 7_-_Looping-SEP-2015

Programming Langguage 6

Private Sub Form_ActivateDo while counter <10counter =counter+1Print CounterLoopEnd Sub

Do While

Page 7: 7_-_Looping-SEP-2015

Programming Langguage

The Do While Loop7

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.

Page 8: 7_-_Looping-SEP-2015

Programming Langguage

The Do While Loop8

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.

Page 9: 7_-_Looping-SEP-2015

Programming Langguage

The Do While Loop9

Private Sub Command1_Click()Dim InpName As StringDo While InpName <> "Done" InpName = InputBox("Type a name or Done to quit.") If InpName <> "Done" Then Label1.Caption = InpName End IfLoopEnd Sub

For example, this Do loop consists of statements that process input until the user enters the word Done:

Page 10: 7_-_Looping-SEP-2015

Programming Langguage

The Do While Loop10

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 doesn’t 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.

Page 11: 7_-_Looping-SEP-2015

Programming Langguage

The Do While Loop11

Private Sub Command1_Click()Dim StrAge As StringDim Age As IntegerStrAge = InputBox("How old are you?", "Age Ask")If (StrAge = "") Then EndEnd IfAge = 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)LoopEnd Sub

Page 12: 7_-_Looping-SEP-2015

Programming Langguage

The Do Until Loop12

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

Page 13: 7_-_Looping-SEP-2015

Programming Langguage

The Do Until Loop13

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.

Page 14: 7_-_Looping-SEP-2015

Programming Langguage 14

Private Sub Form_Activate()Do Counter = Counter + 1 Print CounterLoop Until Counter = 10

End Sub

Page 15: 7_-_Looping-SEP-2015

Programming Langguage 15

Private Sub Form_Activate()Do Until Counter = 10Counter = Counter + 1Print CounterLoopEnd Sub

Page 16: 7_-_Looping-SEP-2015

Programming Langguage 16

Dim n, sum As Integer

Private Sub Form_Activate()List1.AddItem "n" & vbTab & "sum"Do Until n = 100n = n + 1sum = sum + nList1.AddItem n & vbTab & sumLoopEnd Sub

Private Sub Form_Load ()n = 0sum = 0End Sub

Page 17: 7_-_Looping-SEP-2015

Programming Langguage 17

Page 18: 7_-_Looping-SEP-2015

Programming Langguage

The Do Until Loop18

Private Sub Command1_Click()Dim InpName As StringDo Until InpName = "Done" InpName = InputBox("Type a name or Done to quit.") If InpName <> "Done" Then Label1.Caption = InpName End IfLoopEnd Sub

For example, this Do loop consists of statements that process input until the user enters the word Done:

Page 19: 7_-_Looping-SEP-2015

Programming Langguage

The Do Until Loop19

Private Sub Command1_Click()Dim StrAge As StringDim Age As IntegerStrAge = InputBox("How old are you?", "Age Ask")If (StrAge = "") Then EndEnd IfAge = 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)LoopEnd Sub

Page 20: 7_-_Looping-SEP-2015

Programming Langguage

The Other Do Loops20

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.

Page 21: 7_-_Looping-SEP-2015

Programming Langguage

The Other Do Loops21

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)

Page 22: 7_-_Looping-SEP-2015

Programming Langguage

The Other Do Loops22

Private Sub Command1_Click()Dim InpName As StringDo InpName = InputBox("Type a name or Done to quit.") If InpName <> "Done" Then Label1.Caption = InpName End IfLoop While InpName <> "Done"End Sub

Page 23: 7_-_Looping-SEP-2015

Programming Langguage

The Other Do Loops23

Private Sub Command1_Click()Dim InpName As StringDo InpName = InputBox("Type a name or Done to quit.") If InpName <> "Done" Then Label1.Caption = InpName End IfLoop Until InpName = "Done"End Sub

Page 24: 7_-_Looping-SEP-2015

Programming Langguage

The Other Do Loops24

Private Sub Command1_Click()Dim StrAge As StringDim Age As IntegerDo 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 IfLoop While ((Age < 10) Or (Age > 99))End Sub

Page 25: 7_-_Looping-SEP-2015

Programming Langguage

The Other Do Loops25

Private Sub Command1_Click()Dim StrAge As StringDim Age As IntegerDo 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 IfLoop Until ((Age >= 10) And (Age <= 99))End Sub

Page 26: 7_-_Looping-SEP-2015

Programming Langguage

The For Loop26

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 statementsNext CounterVar

Page 27: 7_-_Looping-SEP-2015

FOR COUNTER=STARTNUM TO

ENDNUM (STEP INCREMENT) ONE OR MORE VISUAL BASIC STATEMENTSNEXT COUNTER

For

Programming Langguage

27

Page 28: 7_-_Looping-SEP-2015

For…Next Statement

For I = 0 To 100 Step 2statement…statement…

Next I

28Programming Langguage

Page 29: 7_-_Looping-SEP-2015

Flow-Chart loop : For

for loop

Statement ;

.

.

end

29Programming Langguage

Page 30: 7_-_Looping-SEP-2015

Contoh program30

Programming Langguage

Page 31: 7_-_Looping-SEP-2015

Programming Langguage 31

Example 10.1This 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 CounterNext counter

End Sub

Page 32: 7_-_Looping-SEP-2015

Programming Langguage 32

Page 33: 7_-_Looping-SEP-2015

Programming Langguage 33

Page 34: 7_-_Looping-SEP-2015

Programming Langguage 34

Example 10.2Private Sub Form_Activate ()For counter=0 to 100 step 10Print counterNext counterEnd Sub

Page 35: 7_-_Looping-SEP-2015

Programming Langguage

The For Loop35

Add the numbers from 1 to 10. Private Sub Command1_Click()Dim Sum As IntegerDim Number As IntegerSum = 0

For Number = 1 To 10 Sum = Sum + NumberNext Number

Label1.Caption = SumEnd Sub

Page 36: 7_-_Looping-SEP-2015

Programming Langguage

The For Loop36

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 IntegerFor c = 1 To 20 Step 4 MsgBox "This is a message box"Next c End Sub

Page 37: 7_-_Looping-SEP-2015

Programming Langguage

The For Loop37

If you specify a negative Step value, Visual Basic counts down. Private Sub Command1_Click()Dim c As IntegerFor c = 20 To 1 Step -4 MsgBox "This is a message box"Next c End Sub

Page 38: 7_-_Looping-SEP-2015

Programming Langguage

The For Loop38

Use the Print method in a For…Next loop to display output on a form:Private Sub Command1_Click()Dim i As IntegerFor i = 1 To 10

Print "Line"; iNext iEnd Sub

Symbol Behavior

comma (,) Places the elements one tab field apart

semicolon (;) Places elements side by side

Page 39: 7_-_Looping-SEP-2015

Programming Langguage

The For Loop39

The example below shows how a For…Next loop can change the text size on a form by changing the form's FontSize property:Private Sub Command1_Click()Dim I As IntegerFor I = I To 10 Print "Line"; I FontSize = 10 + INext IEnd Sub

Page 40: 7_-_Looping-SEP-2015

Programming Langguage

Exit For Statement40

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.

Page 41: 7_-_Looping-SEP-2015

Programming Langguage

Exit For Statement41

Private Sub Command1_Click()Dim I As IntegerFor I = I To 10 InpName = InputBox("Type a name or Done to quit.") If InpName = "Done" Then Exit For Print InpNameNext IEnd Sub

Page 42: 7_-_Looping-SEP-2015

Programming Langguage 42

For counter1=startNumber to endNumber (Step increment) For counter2=startNumber to endNumber (Step increment) One or more VISUAL BASIC statements Next counter2Next counter1

NESTED LOOP

Page 43: 7_-_Looping-SEP-2015

Programming Langguage 43

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

Page 44: 7_-_Looping-SEP-2015

Programming Langguage 44