3
 9/22/12 Vi sual Basi c l esson 9: Loopi ng 1/3 www.vbtutor.net/vb6/lesson9.html Ads by G oogle  Excel Tutorial  Design Tutorial  Visual Basic  VB  BASIC Programming Liberty BASIC gives you a power toolkit for Windows progr amming! www.libertybasic.com VisualBasic 2008 Tutorial Learn Vis ual Basic 2008 in your own. Free Visual Basic Tutorials. f1visualbasic.com Top15 Accounting Software 2012 Top 15 Accounti ng Softw are Rankings. Dow nload Free Report. Business-Software.com/2012Report PowerBasic vs VisualBasic Faster. No Run-Times. No Bloat! CGI, Macros, ASM, Reg Expressions www.powerbasic.com

Visual Basic Lesson 9_ Looping

Embed Size (px)

DESCRIPTION

Visual Basic Lesson 9_ Looping

Citation preview

  • 9/22/12 Visual Basic lesson 9: Looping

    1/3www.vbtutor.net/vb6/lesson9.html

    Search

    Lesson 9: Looping

    Custom Search

    Another procedure that involves decision makingis looping. Visual Basic allows a procedure to berepeated many times until a condition or a set ofconditions is fulfilled. This is generallycalled looping . Looping is a very useful feature of VisualBasic because it makes repetitive works easier. Thereare two kinds of loops in Visual Basic, the Do...Loop and the For.......Next loop

    9.1 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

    9.2 Exiting the Loop

    Sometime we need exit to exit a loop prematurely

    Example 9.1

    Do while counter 1000.

    The above example can be rewritten as

    Do

    num.Text=counter counter=counter+1

    Loop until counter>1000

    Example 9.2

    Dim sum, n As Integer

    Private Sub Form_Activate()

    List1.AddItem "n" & vbTab & "sum"

    Do

    n = n + 1

    Sum = Sum + n

    List1.AddItem n & vbTab & Sum

    If n = 100 Then

    Exit Do

    End If

    Loop

    End Sub

    Explanation

    In the above example, we compute thesummation of 1+2+3+4++100. In thedesign stage, you need to insert a ListBoxinto the form for displaying the output,named List1. The program uses theAddItem method to populate the ListBox.The statement List1.AddItem "n" & vbTab& "sum" will display the headings in theListBox, where it uses the vbTab functionto create a space between the headings nand sum.

    Example 9.3 a

    For counter=1 to 10

    display.Text=counter

    Example 9.3 b

    For counter=1 to1000 step 10

    Ads by Google Excel Tutorial Design Tutorial Visual Basic VB

    BASIC ProgrammingLiberty BASIC gives you a power toolkit forWindows programming!www.libertybasic.com

    VisualBasic 2008 TutorialLearn Visual Basic 2008 in your own. Free VisualBasic Tutorials.f1visualbasic.com

    Top15 Accounting Software2012 Top 15 Accounting Software Rankings.Download Free Report.Business-Software.com/2012Report

    PowerBasic vs VisualBasicFaster. No Run-Times. No Bloat! CGI, Macros, ASM,Reg Expressionswww.powerbasic.com

  • 9/22/12 Visual Basic lesson 9: Looping

    2/3www.vbtutor.net/vb6/lesson9.html

    Sometime we need exit to exit a loop prematurelybecause of a certain condition is fulfilled. Thesyntax to use is known as Exit Do. You canexamine Example 9.2 for its usage.

    9.3 For....Next Loop

    The format is:

    For counter=startNumber toendNumber (Step increment)

    One or more VB statements

    Next

    Please refer to example 9.3a,9.3b and 9.3 c for itsusage.

    Sometimes the user might want to get out fromthe loop before the whole repetitive process isexecuted, the command to use is Exit For. Toexit a For.Next Loop, you can place the Exit Forstatement within the loop; and it is normally usedtogether with the If..Then statement. Letsexamine example 9.3 d.

    display.Text=counter

    Next

    1000 step 10

    counter=counter+1

    Next

    Example 9.3 c

    For counter=1000 to 5step -5

    counter=counter-10

    Next

    *Notice that incrementcan be negative

    Example 9.3 d

    Private SubForm_Activate( )

    For n=1to 10

    If n>6then

    Exit For

    End If

    Else

    Print n

    End If

    End Sub

    < Next Lesson>

    Copyright 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: [email protected]

    [Privacy Policy]

    Ads by Google Excel Tutorial Design Tutorial Visual Basic VB

  • 9/22/12 Visual Basic lesson 9: Looping

    3/3www.vbtutor.net/vb6/lesson9.html