CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    what equivalent to "continue" in C++

    Hello, I have a for...next loop, and wanna have a "continue" statement if some condition is matched, but I can't find it on MSDN, plz tip me, a newnewbie in VB, tks.

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: what equivalent to "continue" in C++

    Old Visual Basic does not have a Continue statement
    But in case of VB.Net
    The Continue Statement is the New Feature in VB.NET 2005 -

    for more detils regarding .net just read following article
    http://www.knowdotnet.com/articles/c...statement.html

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: what equivalent to "continue" in C++

    There is no continue in VB.NET 2003. So you would have to manage this with an If statement. Something like this
    Code:
    Dim loopCounter As Long
            For loopCounter = 1 To 100
                 'put your condition here
            Next

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: what equivalent to "continue" in C++

    If you need a continue there's a problem with the structure of your loop and or program, cause I rarely have to use a continue statement and even then, I try to change my loop so I don't have to use it.
    Nicolas Bohemier

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: what equivalent to "continue" in C++

    Quote Originally Posted by Boumxyz2
    If you need a continue there's a problem with the structure of your loop and or program
    Why's that?

    Quote Originally Posted by Boumxyz2
    cause I rarely have to use a continue statement and even then, I try to change my loop so I don't have to use it.
    It try to picture some situations where I could replace the continue statement and all I see is a lot of nested if statements (or gotos). Are that any better?

    - petter

  6. #6
    Join Date
    Jan 2006
    Posts
    293

    Re: what equivalent to "continue" in C++

    Maybe it will be easier if you type out in pseudocode what you are trying to accomplish in your for...next loop... instead of everyone guessing

  7. #7
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    Re: what equivalent to "continue" in C++

    Quote Originally Posted by gigemboy
    Maybe it will be easier if you type out in pseudocode what you are trying to accomplish in your for...next loop... instead of everyone guessing
    Indeed, it's not that big a problem, I just wonder whether there is anything equivalent to "continue" (matter of syntax) in VB.NET. I solved it with goto

    for each i as integer = 0 to limit
    if condition = true then
    goto label1
    end if

    'do the rest here
    '............................
    'them comes the label
    label1:
    next i

  8. #8
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Re: what equivalent to "continue" in C++

    If I understand you correct, you wish to skip a huge lump of code if your condition is met?

    Then why not place all the code in your for-loop inside an if-condition with the negated condition, so it only runs when you want to, instead of skipping it when you don't want it to run?

    Something along the line:
    Code:
    for each i as integer = 0 to limit
    if not condition = true then
    'do the rest here
    end if
    next i
    Alternative you could fragment your code into several methods (functions/subs) and do the various parts when ever you want it to determined by if-conditions.

  9. #9
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: what equivalent to "continue" in C++

    Never use goto. I repeat: never use goto. I repeat again: never use goto. If that's not enough I can repeat it as many times you want.

    how about doing:
    Code:
    for each i as integer = 0 to limit
       if not condition then 
            i=limit +1
       else
            'do the rest here
       end if
    next i
    Or:
    Code:
    dim i as integer=0
    while condition and i<limit
         'do the rest here
         i += 1
    end while
    I'm sure you can do this in at least 10 other ways (and not using goto)

  10. #10
    Join Date
    Oct 2004
    Location
    Ho Chi Minh, Vietnam
    Posts
    126

    Re: what equivalent to "continue" in C++

    I know there are so many complaints about "goto" but sometimes it is still needed to make code become intuitive. Any better solution for this

    Code:
            For i As Integer = 0 To limit
                If conditionA Then
                    job1()
                    If conditionB Then
                        job2()
                        If conditionC Then
                            job3()
                            GoTo label1
                        End If
                        job22()
                    End If
                    job11()
                End If
                job0()
    label1:
            Next i
    Last edited by choconlangthang; January 26th, 2006 at 11:47 AM.

  11. #11
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: what equivalent to "continue" in C++

    First, the code is not very well-designed. It is not very readable, very hard to maintain, and it is not open for extensions. It is just not very object-oriented. This kind of code is very C-stylish. It is plainfully old fashioned. I would strongly advice to redesign the whole lot.

    Second, do you really want to get into the old discussion of goto? Is it not a common knowledge that goto breaks the flow of the code? If you want to debug can you easily tell how did you get to the label? I programmed with VB 6 , and I remember the happy days of On Error Goto statements. I sure as hell don't want to get back to those days. Lets not waste time on goto. I strongly advise against it. I say naver use it. If you know what's good for you - stay away from goto. But hey, be my guest.

    Third:
    Code:
     For i As Integer = 0 To limit
                dim flag as boolean = true
                If conditionA Then
                    job1()
                    If conditionB Then
                        job2()
                        If conditionC Then
                            job3()
                            flag = false
                        End If
                        if flag then job22()
                    End If
                   if flag then  job11()
                End If
                if flag then job0()
            Next i
    The code is still very ugly (but it was ugly in the first place!!!). At least we don't have this goto in the code (thank god for that).

  12. #12
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: what equivalent to "continue" in C++

    Quote Originally Posted by choconlangthang
    I know there are so many complaints about "goto" but sometimes it is still needed to make code become intuitive. Any better solution for this

    Code:
            For i As Integer = 0 To limit
                If conditionA Then
                    job1()
                    If conditionB Then
                        job2()
                        If conditionC Then
                            job3()
                            GoTo label1
                        End If
                        job22()
                    End If
                    job11()
                End If
                job0()
    label1:
            Next i
    Personally, since you have all the subroutines anyway, why not make a few boolean functions.
    Code:
        Private Sub DoIt()
            Dim Limit As Integer
            Dim ConditionA As Boolean
            Dim ConditionB As Boolean
            Dim ConditionC As Boolean
            For i As Integer = 0 To Limit
                Job0(ConditionA, ConditionB, ConditionC)
            Next i
    
        End Sub
        Private Sub Job0(ByVal conditiona, ByVal conditionb, ByVal conditionc)
            If conditiona Then
                If Job1(conditionb, conditionc) Then
                    Exit Sub
                Else
                    Job11()
                End If
            End If
            ' do job0 stuff
        End Sub
        Private Sub Job11()
            ' do job11 stuff
        End Sub
        Private Function Job1(ByVal conditionb, ByVal conditionc) As Boolean
            If conditionb Then
                Return Job2(conditionc)
            End If
            Job11()
        End Function
        Private Function Job2(ByVal conditionc) As Boolean
            If conditionc Then
                Job3()
                Return True
            End If
            Job22()
        End Function
        Private Sub Job22()
            ' do stuff for job22
        End Sub
        Private Sub Job3()
            'do job3 stuff
        End Sub

    NOTE: I just wrote this, I didn't try it but I think it would work.

  13. #13
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: what equivalent to "continue" in C++

    u can introduce a flag that change when the third check is true
    Code:
    If conditionC Then
          job3()
          flag=false
    enclose the function that shd not run
    Code:
    if flag then  
           job22()
    end if
    Last edited by aniskhan; January 26th, 2006 at 03:00 PM.

  14. #14
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: what equivalent to "continue" in C++

    OMG this is more complicated to read
    Code:
            For i As Integer = 0 To limit
                If conditionA Then
                    job1()
                    If conditionB Then
                        job2()
                        If conditionC Then
                            job3()
                            GoTo label1
                        End If
                        job22()
                    End If
                    job11()
                End If
                job0()
    label1:
            Next i
    than
    Code:
    For i As Integer = 0 To limit
        if conditionA and Conditionb and ConditionC then
    	job1()
    	job2()
    	job3()
        elseif conditionA and ConditionB and not ConditionC then
    	job1()
    	job2()
    	job22()
    	job11()
                    job0()
        elseif conditionA and not conditionB and not conditionC then
    	job1()
    	job11()
                    job0()
        elseif not conditionA then
                   job0()
        endif
    next i
    Last edited by Boumxyz2; January 26th, 2006 at 04:42 PM.
    Nicolas Bohemier

  15. #15
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: what equivalent to "continue" in C++

    It's interesting to see all the different coding solutions.

    choconlangthang,

    Yes you are allowed to use GOTO but most programmers avoid them at all costs. There should always be a way around using a goto. Please try to find the that way.

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

Featured