Results 1 to 14 of 14

Thread: What code in VB that used like 'break' and 'continue' in C?

  1. #1

    Thread Starter
    Hyperactive Member solar115's Avatar
    Join Date
    Apr 2002
    Location
    Thailand
    Posts
    268

    What code in VB that used like 'break' and 'continue' in C?

    Now I code some loop that must be use some code to 'continue' and 'break'. The 2 word I say is found on C but on VB I'm not found any word (I think 'break' may be replace with 'Exit For' in VB). What word that I can use in VB?
    ^solaris^

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    If you post an example of what you are talking about in C, I could probably help.

    Please put the following tags around your C code: [code] [/code]
    ~seaweed

  3. #3

    Thread Starter
    Hyperactive Member solar115's Avatar
    Join Date
    Apr 2002
    Location
    Thailand
    Posts
    268
    If anything wrong Pls tel me and may be you can correct it...

    For C
    a=50;
    for(i=1;i<10;i++)
    {
    if (a%i==0) break;
    if (a%i==1) continue;
    a=a+1;
    }

    For VB
    a=50
    for a=1 to 9
    if a mod i = 0 then Exit For
    if a mod i = 1 then ??????
    a=a+1
    next
    Last edited by solar115; Mar 9th, 2003 at 09:29 PM.
    ^solaris^

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    I think I understand. I don't really know what the continue statement does, however. I don't think you need it at all.
    VB Code:
    1. Dim i As Integer
    2. Dim a As Integer
    3.  
    4. a = 50
    5.  
    6. For i = 1 To 9
    7.     ' The following will exit this loop
    8.     If a Mod i = 0 Then Exit For
    9.  
    10.     If a Mod i = 1 Then
    11.         ' Do something here
    12.         ' and here if you want...
    13.     End If
    14.    
    15.     a = a + 1
    16. Next
    The idea of break, as you correctly pointed out, is "Exit". You do have to follow it with the construct you want to exit, however. In the above example, we exit the For loop with Exit For. You can also do the following:
    VB Code:
    1. Dim i As Integer
    2. Dim a As Integer
    3.  
    4. a = 50
    5.  
    6. Do
    7.     i = i + 1
    8.    
    9.     ' The following will exit this loop
    10.     If a Mod i = 0 Then Exit Do
    11.    
    12.     If a Mod i = 1 Then
    13.         ' Do something here
    14.         ' and here if you want...
    15.     End If
    16.    
    17.     a = a + 1
    18. Loop Until i = 9
    Similarly, you can exit functions and subs with
    VB Code:
    1. ' Inside a sub routine:
    2. Public Sub Foo(strIn As String)
    3.     If Len(strIn) = 0 Then Exit Sub
    4.  
    5.     ' Do stuff here
    6. End Sub
    7.  
    8. ' Or inside a function
    9. Public Function Foo(strIn As String) As String
    10.     If Len(strIn) = 0 Then
    11.         Exit Function
    12.     End If
    13.  
    14.     ' Do stuff here
    15. End Function
    Hope that helps!
    ~seaweed

  5. #5

    Thread Starter
    Hyperactive Member solar115's Avatar
    Join Date
    Apr 2002
    Location
    Thailand
    Posts
    268
    please not that 'continue' will be go to do in next index with the same loop. In example , is 'continue' is worked the code a=a+1 will not work. But from your example, I work in any case.
    ^solaris^

  6. #6
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Oh, I see. Since there is no similar command in VB, you just need to structure your constructs around the "continue" case. In other words, use an if statement to wrap the code that you want to execute in all cases except where you would "continue" in C. This way, the effect is similar to "continue" in that nothing further happens inside the loop for this iteration, but the loop is not exited.

    For example:
    VB Code:
    1. Dim i As Integer
    2. Dim a As Integer
    3.  
    4. a = 50
    5.  
    6. For i = 1 To 9
    7.     If a Mod i = 0 Then Exit For
    8.  
    9.     If a Mod i <> 1 Then
    10.         a = a + 1
    11.     End If
    12. Next
    Or maybe this syntax is easier to understand visually (although it's not really logical to have an empty If block):
    VB Code:
    1. Dim i As Integer
    2. Dim a As Integer
    3.  
    4. a = 50
    5.  
    6. For i = 1 To 9
    7.     If a Mod i = 0 Then Exit For
    8.  
    9.     If a Mod i = 1 Then
    10.         ' Continue (do nothing, but don't exit the loop)
    11.     Else
    12.         a = a + 1
    13.     End If
    14. Next
    Last edited by seaweed; Mar 9th, 2003 at 10:17 PM.
    ~seaweed

  7. #7

    Thread Starter
    Hyperactive Member solar115's Avatar
    Join Date
    Apr 2002
    Location
    Thailand
    Posts
    268
    I think may my question is not cleared. I ask for some key word or command or function for this casee. I can think of any algorithm that used for decrease the problem, SURE. So may somebody know that VB has some command like 'continue' in C language, Or Not?
    ^solaris^

  8. #8
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by solar115
    I think may my question is not cleared. I ask for some key word or command or function for this casee. I can think of any algorithm that used for decrease the problem, SURE. So may somebody know that VB has some command like 'continue' in C language, Or Not?
    There is no counterpart of 'continue' in VB !!!!

  9. #9
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    So may somebody know that VB has some command like 'continue' in C language, Or Not?
    not

  10. #10
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    I cannot think of any similar function in VB. Can you tell me a case where you would need such a function where using an if statement doesn't work?
    ~seaweed

  11. #11
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    You could always use a nasty goto -- that's what Exit For, break, and continue are.

  12. #12
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048
    this what u want
    VB Code:
    1. a = 50
    2. For a = 1 To 9
    3. If a Mod i = 0 Then Exit For
    4. If a Mod i = 1 Then a = a + 1
    5. Next

    if u think vb is going to translate word for word from c without behavioral differences u r simply wrong my friend

  13. #13
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Uhh what exactly does continue do in C? Continue the loop? Surely not... hmm WEll it can't continue the rest of the code casue thats what Break;/Exit For does.... hmm odd

    from what i can tell this is what you want

    VB Code:
    1. a=50
    2. for i=1 to 9
    3. if a mod i = 0 then Exit For
    4. a=a+1
    5. next i
    6. 'that looks about right to me...




    Now let me look at your C code and try and decifer what you ment according to it.

    VB Code:
    1. a=50; a=50, gotcha
    2. for(i=1;i<10;i++){' for i = 1 to 10
    3. if (a%i==0) break; 'if "a" times(not sure what % means in C)"i" = 0 then exit for
    4. if (a%i==1) continue; 'if "a" times "i" = 1 then a =a+1
    5. a=a+1;
    6. }

    Is that what you mean? I know VB and PHP really well, PHP looks pretty dang close to C but i am not following your logic here...
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  14. #14
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    VB Code:
    1. Dim s As String
    2.     Dim i As Integer
    3.  
    4.  
    5.     For i = 0 To 10
    6.         s = s & Chr(i + 66)
    7.    
    8.         If i = 4 Then GoTo CONTINUE 'no VB equivalent
    9.        
    10.         s = s & Chr(i + 66)
    11.        
    12.         If i = 8 Then GoTo EXIT_FOR 'the same as Exit For
    13. CONTINUE:
    14.     Next
    15. EXIT_FOR:
    continue skips the rest of the current iteration of a loop -- it does NOT increment the counter variable twice! Remember, the Next statement is what increments the counter variable, so putting "i = i + 1" right before the "Next" will increment i twice.

    Keep in mind that the best programming techniques avoid any sort of goto whenever reasonable. Exit For, Exit Sub/Exit Function, break/continue (except for switch statements, which the break is required for) are all just glorified versions of goto, which break the strictest programming "rules" (I forget the correct term).

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