Rabu, 04 Februari 2009

Visual Basic 2008 Tutorial
Lesson 11: Looping




Visual Basic 2008 allows a procedure to be repeated as many times as long as the processor could support. This is generally called looping . Looping is required when we need to process something repetitively until a certain condition is met. For example, we can design a program that adds a series of numbers until it exceed a certain value, or a program that asks the user to enter data repeatedly until he/she keys in the word 'Finish'. In Visual Basic 2008, we have three types of Loops, they are the For.....Next loop, the Do loop. and the While.....End while loop

11.1 For....Next Loop
The format is:
For counter=startNumber to endNumber (Step increment)

One or more VB statements

Next

Sometimes the user might want to get out from the loop before the whole repetitive process is executed, the command to use is Exit For. To exit a For….Next Loop, you can place the Exit For statement within the loop; and it is normally used together with the If…..Then… statement. For its application, you can refer to example 11.1 d.


Example 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.
Example 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+......

Example 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-..........

¡¡
Example 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

¡¡

The formats are

¡¡

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

¡¡

* Exiting the Loop

Sometime we need exit to exit a loop prematurely because of a certain condition is fulfilled. The syntax to use is known as Exit Do. Lets examine the following example


Example 11.2(a)

¡¡

Do while counter <=1000

TextBox1.Text=counter

counter +=1

Loop

¡¡

* The above example will keep on adding until counter >1000.

¡¡

The above example can be rewritten as

Do

TextBox1.Text=counter
counter+=1

Loop until counter>1000

¡¡

¡¡

Example 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
Do
n += 1
sum += n
ListBox1.Items.Add(n & vbTab & sum)
If n = 100 Then
Exit Do
End If
Loop Sub

¡¡

In the above example, we find the summation of 1+2+3+4+……+100. In the design stage, you need to insert a ListBox into the form for displaying the output, named List1. The program uses the AddItem method to populate the ListBox. The statement ListBox1.Items.Add(n & vbTab & sum) will display the headings in the ListBox, where it uses the vbTab function to create a space between the headings n and sum.






source : http://www.vbtutor.net/vb2008/vb2008tutor.html

--------------------------------------------------------------

How To Add Controls

How to play audio vb.net

How to move VisualStudio.NET

How To Print vb2005

Remove Item Importing Textfile

koneksialldb

Loading image files

load multi image

looping vb2008

Managing SQLServer2005

Dokter PC

Trik Domain Gratis

Trik Download game free

Trik Download Video

Broadband-internet

Trik Buat Antivirus

Trik-download-dari-google

Trik Cara Melacak IP

Trik Choose Phone

Trik woodwork-stores