|
-
Mar 9th, 2003, 08:48 PM
#1
Thread Starter
Hyperactive Member
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?
-
Mar 9th, 2003, 09:01 PM
#2
Frenzied Member
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]
-
Mar 9th, 2003, 09:09 PM
#3
Thread Starter
Hyperactive Member
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^
-
Mar 9th, 2003, 09:56 PM
#4
Frenzied Member
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:
Dim i As Integer
Dim a As Integer
a = 50
For i = 1 To 9
' The following will exit this loop
If a Mod i = 0 Then Exit For
If a Mod i = 1 Then
' Do something here
' and here if you want...
End If
a = a + 1
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:
Dim i As Integer
Dim a As Integer
a = 50
Do
i = i + 1
' The following will exit this loop
If a Mod i = 0 Then Exit Do
If a Mod i = 1 Then
' Do something here
' and here if you want...
End If
a = a + 1
Loop Until i = 9
Similarly, you can exit functions and subs with
VB Code:
' Inside a sub routine:
Public Sub Foo(strIn As String)
If Len(strIn) = 0 Then Exit Sub
' Do stuff here
End Sub
' Or inside a function
Public Function Foo(strIn As String) As String
If Len(strIn) = 0 Then
Exit Function
End If
' Do stuff here
End Function
Hope that helps!
-
Mar 9th, 2003, 10:03 PM
#5
Thread Starter
Hyperactive Member
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.
-
Mar 9th, 2003, 10:13 PM
#6
Frenzied Member
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:
Dim i As Integer
Dim a As Integer
a = 50
For i = 1 To 9
If a Mod i = 0 Then Exit For
If a Mod i <> 1 Then
a = a + 1
End If
Next
Or maybe this syntax is easier to understand visually (although it's not really logical to have an empty If block):
VB Code:
Dim i As Integer
Dim a As Integer
a = 50
For i = 1 To 9
If a Mod i = 0 Then Exit For
If a Mod i = 1 Then
' Continue (do nothing, but don't exit the loop)
Else
a = a + 1
End If
Next
Last edited by seaweed; Mar 9th, 2003 at 10:17 PM.
~seaweed
-
Mar 10th, 2003, 08:51 PM
#7
Thread Starter
Hyperactive Member
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?
-
Mar 10th, 2003, 08:57 PM
#8
Let me in ..
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 !!!!
-
Mar 10th, 2003, 08:59 PM
#9
PowerPoster
So may somebody know that VB has some command like 'continue' in C language, Or Not?
not
-
Mar 10th, 2003, 09:03 PM
#10
Frenzied Member
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?
-
Mar 10th, 2003, 11:35 PM
#11
Fanatic Member
You could always use a nasty goto -- that's what Exit For, break, and continue are.
-
Mar 11th, 2003, 01:30 AM
#12
Frenzied Member
this what u want
VB Code:
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
if u think vb is going to translate word for word from c without behavioral differences u r simply wrong my friend
-
Mar 11th, 2003, 02:52 AM
#13
PowerPoster
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:
a=50
for i=1 to 9
if a mod i = 0 then Exit For
a=a+1
next i
'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:
a=50; a=50, gotcha
for(i=1;i<10;i++){' for i = 1 to 10
if (a%i==0) break; 'if "a" times(not sure what % means in C)"i" = 0 then exit for
if (a%i==1) continue; 'if "a" times "i" = 1 then a =a+1
a=a+1;
}
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.

-
Mar 11th, 2003, 07:22 AM
#14
Fanatic Member
VB Code:
Dim s As String
Dim i As Integer
For i = 0 To 10
s = s & Chr(i + 66)
If i = 4 Then GoTo CONTINUE 'no VB equivalent
s = s & Chr(i + 66)
If i = 8 Then GoTo EXIT_FOR 'the same as Exit For
CONTINUE:
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|