Results 1 to 4 of 4

Thread: The Do and loop commands

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    879

    Angry

    Can any way explain how do and loop work and how they go together. Please also include some source for example! Thanks
    Visual Basic 6.0
    Visual C++ 5
    Delphi 5


  2. #2
    Guest
    Do...Loop Statement

    Repeats a block of statements while a condition is True or until a condition becomes True.

    Syntax

    Do [{While | Until} condition]
    [statements]
    [Exit Do]
    [statements]
    Loop

    Or, you can use this equally valid syntax:

    Do
    [statements]
    [Exit Do]
    [statements]
    Loop [{While | Until} condition]

    The Do...Loop statement syntax has these parts:

    Part Description
    [/quote]
    condition Numeric or string expression that is True or False. If condition is Null, condition is treated as False.
    statements One or more statements that are repeated while or until condition is True.

    Remarks

    The Exit Do can only be used within a Do...Loop control structure to provide an alternate way to exit a Do...Loop. Any number of Exit Do statements may be placed anywhere in the Do...Loop. Often used with the evaluation of some condition (for example, If...Then), Exit Do
    transfers control to the statement immediately following the Loop.
    When used within nested Do...Loop statements, Exit Do transfers control to the loop that is one nested level above the loop where it occurs.


    *Taken from the VB Help file

    Here is an example:

    Code:
    'Make a Message box pop up 5 times
    
    Dim msgcount As Integer
    
    Private Sub Form_Load()
    msgcount = 0
    End Sub
    
    Private Sub Command1_Click()
    'Right now, msgcount = 0
    Do Until msgcount = 5 'Do Until msgcount has showed 5 times
    msgcount = msgcount + 1 'add msgcount + 1 each time
    MsgBox msgcount 'msgcount
    DoEvents 'wait until process is preceded before continuing on to the next msgbox
    Loop 'loop it until msgcount = 5
    End Sub

  3. #3
    Lively Member
    Join Date
    Sep 2000
    Location
    NC, USA
    Posts
    102

    Lightbulb some starter info

    I don't have time to include a good sample right now but I will say I think you have to use While or Until with Do and then set a criteria sorta like

    Do Until Variable = 5
    Variable = Variable + 1
    Loop

    or

    Do While Variable < 4
    Variable = Variable + 1
    Loop

    Both of these will loop 5 times and then when the criteria is met it will exit the loop.


    {how embarassing, I just finished little post when I saw yours Mr Gates sir, hehe}

    [Edited by KillemAll on 09-28-2000 at 07:45 PM]

  4. #4
    Guest
    KillemAll {how embarassing, I just finished little post when I saw yours Mr Gates sir, hehe}
    All posts are better than none .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width