Visual Basic 2010 memungkinkan prosedur harus diulang kali lebih banyak selama prosesor dan memori dapat mendukung. Hal ini umumnya disebut looping. Perulangan diperlukan bila kita perlu memproses sesuatu berulang-ulang sampai kondisi tertentu terpenuhi. Sebagai contoh, kita dapat merancang sebuah program yang menambahkan serangkaian nomor sampai jumlahnya melebihi nilai tertentu, atau sebuah program yang meminta user untuk memasukkan data berulang kali sampai ia / dia kunci dalam 'Finish' kata. Dalam Visual Basic 2010, kami memiliki tiga jenis Loops, mereka adalah the For.....Next loop, the Do loop. and the While.....End while loop
11.1 For Next Loop
Formatnya adalah
For counter=startNumber to endNumber (Step increment)
One or more VB statements
Next
One or more VB statements
Next
Kadang-kadang pengguna mungkin ingin keluar dari loop sebelum proses berulang keseluruhan dijalankan, perintah untuk menggunakan Exit For. Untuk keluar dari Loop Berikutnya Untuk ....., Anda dapat menempatkan Exit Untuk pernyataan dalam loop, dan itu biasanya digunakan bersama dengan If....Then.....sstatement Untuk aplikasinya, Anda bisa lihat contoh 11,1 d.
Contoh 11.1 a
Dim counter as Integer
For counter=1 to 10
ListBox1.Items.Add (counter)
Next
* The program will enter number 1 to 10 into the list box.
Dim counter as Integer
For counter=1 to 10
ListBox1.Items.Add (counter)
Next
* The program will enter number 1 to 10 into the list box.
Contoh 11.1b
Dim counter , sum As Integer
For counter=1 to 100 step 10
sum+=counter
ListBox1.Items.Add (sum)
Next
* The program will calculate the sum of the numbers as follows:
sum=0+10+20+30+40+......
Contoh 11.1c
Dim counter, sum As Integer
sum = 1000
For counter = 100 To 5 Step -5
sum - = counter
ListBox1.Items.Add(sum)
Next
*Notice that increment can be negative.
The program will compute the
subtraction as follow:
1000-100-95-90-..........
Contoh 11.1d
Dim n as Integer
For n=1 to 10
If n>6 then
Exit For
End If
Else
ListBox1.Items.Add ( n)
Next
End If
Next
The process will stop when n is greater than 6.
11.2 Do Loop
Formatnya adalah :
a) Do While condition
Block of one or more VB statements
Loop
b) Do
Block of one or more VB statements
Loop While condition
c) Do Until condition
Block of one or more VB statements
Loop
d) Do
Block of one or more VB statements
Loop Until condition
* Keluar dari Loop
Kadang kita perlu keluar untuk keluar dari lingkaran sebelum waktunya karena kondisi tertentu terpenuhi. Sintaks untuk menggunakan dikenal sebagai Exit Do. Mari kita memeriksa contoh-contoh berikut :
Contoh 11.2(a)
Do while counter <=1000
TextBox1.Text=counter
counter +=1
Loop
sum = 1000
For counter = 100 To 5 Step -5
sum - = counter
ListBox1.Items.Add(sum)
Next
*Notice that increment can be negative.
The program will compute the
subtraction as follow:
1000-100-95-90-..........
Contoh 11.1d
Dim n as Integer
For n=1 to 10
If n>6 then
Exit For
End If
Else
ListBox1.Items.Add ( n)
Next
End If
Next
The process will stop when n is greater than 6.
11.2 Do Loop
Formatnya adalah :
a) Do While condition
Block of one or more VB statements
Loop
b) Do
Block of one or more VB statements
Loop While condition
c) Do Until condition
Block of one or more VB statements
Loop
d) Do
Block of one or more VB statements
Loop Until condition
* Keluar dari Loop
Kadang kita perlu keluar untuk keluar dari lingkaran sebelum waktunya karena kondisi tertentu terpenuhi. Sintaks untuk menggunakan dikenal sebagai Exit Do. Mari kita memeriksa contoh-contoh berikut :
Contoh 11.2(a)
Do while counter <=1000
TextBox1.Text=counter
counter +=1
Loop
* Contoh di atas akan terus menambahkan sampai counter> 1000.
Contoh di atas dapat ditulis kembali sebagai
Do
TextBox1.Text=counter
counter +=1
Loop until counter>1000
Contoh 11.2(b)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sum, n As Integer
ListBox1.Items.Add("n" & vbTab & "Sum")
ListBox1.Items.Add("----------------------")
Do
n += 1
sum += n
ListBox1.Items.Add(n & vbTab & sum)
If n = 100 Then
Exit Do
End If
Loop
End Sub
contoh di atas, kita menemukan penjumlahan arithmatic dari 1 +2 +3 +4 + ...... +100. Pada tahap desain, Anda harus memasukkan ListBox ke dalam formulir untuk menampilkan output Program ini menggunakan metode Tambahkan ke mengisi ListBox. Pernyataan itu ListBox1.Items.Add ("n" & vbTab & "jumlah") akan menampilkan judul dalam ListBox, di mana ia menggunakan fungsi vbTab untuk menciptakan ruang antara n dan jumlah pos. Pernyataan itu ListBox1.Items.Add (n & vbTab & jumlah) akan menampilkan daftar nomor n dan nilai-nilai penjumlahan arithmatic Output ditampilkan di bawah ini:

11.3 While ...End While Loop
Struktur Sementara .... Sementara End sangat mirip dengan Loop Do. dibutuhkan
format berikut:
While condition
Statements
End While
Loop di atas berarti bahwa sementara kondisi ini tidak terpenuhi, pengulangan akan terus berjalan. Perulangan akan berakhir ketika kondisi terpenuhi.
Contoh 11.3
Dim sum, n As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sum, n As Integer
While n <> 100
n += 1
sum = sum + n
ListBox1.Items.Add(n & vbTab & sum)
End While
End Sub
Sandi Permana
18113221
2KA39
Tulisan 12
Teori Organisasi Umum 1#
Budi Utami, S.E
0 comments :
Posting Komentar