?
Solved

Exiting a loop

Posted on 2004-09-26
3
Medium Priority
?
256 Views
Last Modified: 2010-05-02
Hi:
    I has written code that evalutes test conditions.When coditions are true a message box is displayed. How do I exit the loop immediately after the message box?

   'declare variables
    Dim intRow As Integer, intRow2 As Integer, intRow3 As Integer
    Dim intColumn As Integer, intColumn2 As Integer, intColumn3 As Integer
    Dim intX1 As Integer, intX2 As Integer
    Dim intO1 As Integer, intO2 As Integer
    Dim totalX As Integer, totalO As Integer
   
    'test top diagonal for X and O
    If lblXO(0).Caption = "X" And lblXO(4).Caption = "X" And lblXO(8).Caption = "X" Then
        MsgBox " X wins"
    Else
        If lblXO(0).Caption = "O" And lblXO(4).Caption = "O" And lblXO(8).Caption = "O" Then
            MsgBox " O wins!"
           
        End If
    End If
   
    'test bottom diagonal for X and O
    If lblXO(2).Caption = "X" And lblXO(4).Caption = "X" And lblXO(6).Caption = "X" Then
        MsgBox " X wins!"
       
    Else
        If lblXO(2).Caption = "O" And lblXO(4).Caption = "O" And lblXO(6).Caption = "O" Then
        MsgBox " O wins!"
        End If
    End If
     
   
       
    'loop for rows
    Do While intRow <= 2
       
       
        'loop for columns
        Do While intColumn <= (intRow + 6)
       
        'assign columns
        intColumn2 = intRow + 3
        intColumn3 = intRow + 6
       
        'test columns for X or O
        If lblXO(intRow).Caption = "X" And lblXO(intColumn2).Caption = "X" And lblXO(intColumn3).Caption = "X" Then
           
            MsgBox " X wins!"
                       
         Else
            If lblXO(intRow).Caption = "O" And lblXO(intColumn2).Caption = "O" And lblXO(intColumn3).Caption = "O" Then
             MsgBox " O wins!"
             
            End If
         End If
           
         'assign rows
         intRow2 = intColumn + 1
         intRow3 = intColumn + 2
           
         'test rows for X or O
         If lblXO(intColumn).Caption = "X" And lblXO(intRow2).Caption = "X" And lblXO(intRow3).Caption = "X" Then
            MsgBox " X wins"
         Else
            If lblXO(intColumn).Caption = "O" And lblXO(intRow2).Caption = "O" And lblXO(intRow3).Caption = "O" Then
                MsgBox " O wins!"
            End If
         End If
           
         'counter for columns
         intColumn = intColumn + 3
         
         'loop for columns
         Loop
               
       
    'counter for rows
    intRow = intRow + 1
    intColumn = 0
       
    'loop for rows
    Loop
   
 
   
End Sub




0
Comment
Question by:LearningJava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 400 total points
ID: 12155683
In a Do loop use this line:

Exit Do

In a For .. Next loop:

Exit For
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 total points
ID: 12156625
If you rearrange your conditonal statement a little then you can do it like this:

Private Sub checkForWin()
    'declare variables
    Dim intRow As Integer, intRow2 As Integer, intRow3 As Integer
    Dim intColumn As Integer, intColumn2 As Integer, intColumn3 As Integer
    Dim intX1 As Integer, intX2 As Integer
    Dim intO1 As Integer, intO2 As Integer
    Dim totalX As Integer, totalO As Integer
   
    If lblXO(0).Caption = "X" And lblXO(4).Caption = "X" And lblXO(8).Caption = "X" Then
        'test top diagonal for X and O
        MsgBox " X wins"
    ElseIf lblXO(0).Caption = "O" And lblXO(4).Caption = "O" And lblXO(8).Caption = "O" Then
        MsgBox " O wins!"
    ElseIf lblXO(2).Caption = "X" And lblXO(4).Caption = "X" And lblXO(6).Caption = "X" Then
        'test bottom diagonal for X and O
        MsgBox " X wins!"
    ElseIf lblXO(2).Caption = "O" And lblXO(4).Caption = "O" And lblXO(6).Caption = "O" Then
        MsgBox " O wins!"
    Else
        'loop for rows
        Do While intRow <= 2
            'loop for columns
            Do While intColumn <= (intRow + 6)
   
                'assign columns
                intColumn2 = intRow + 3
                intColumn3 = intRow + 6
       
                'test columns for X or O
                If lblXO(intRow).Caption = "X" And lblXO(intColumn2).Caption = "X" And lblXO(intColumn3).Caption = "X" Then
                    MsgBox " X wins!"
                    Exit Sub
                ElseIf lblXO(intRow).Caption = "O" And lblXO(intColumn2).Caption = "O" And lblXO(intColumn3).Caption = "O" Then
                    MsgBox " O wins!"
                    Exit Sub
                End If
           
                'assign rows
                intRow2 = intColumn + 1
                intRow3 = intColumn + 2
                   
                'test rows for X or O
                If lblXO(intColumn).Caption = "X" And lblXO(intRow2).Caption = "X" And lblXO(intRow3).Caption = "X" Then
                    MsgBox " X wins"
                    Exit Sub
                ElseIf lblXO(intColumn).Caption = "O" And lblXO(intRow2).Caption = "O" And lblXO(intRow3).Caption = "O" Then
                    MsgBox " O wins!"
                    Exit Sub
                End If
           
                'counter for columns
                intColumn = intColumn + 3
         
            'loop for columns
            Loop
               
            'counter for rows
            intRow = intRow + 1
            intColumn = 0

        'loop for rows
        Loop
    End If
End Sub
0
 
LVL 5

Assisted Solution

by:KarcOrigin
KarcOrigin earned 400 total points
ID: 12156701
Use Exit Do to exit from your Do While loop. Using Exit Sub will exit you from the whole procedure and no statement can be fired outside of the Do While... Loop. Consider the example -

Private Sub MySub()
Do While <condition>
  If (<condition>) Then
    MsgBox("I am here")
    Exit Do
  Else
    MsgBox("I am not here")
  End If
Loop
MsgBox("I am no where")
End Sub

If you use Exit Do then you would get two message "I am here" and "I am no where" But if you use Exit Sub you would get only one message "I am here" because of the Exit Sub statement.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question