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?
Printable View
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?
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]
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
I think I understand. I don't really know what the continue statement does, however. I don't think you need it at all.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 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 NextSimilarly, you can exit functions and subs withVB 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 = 9Hope that helps!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
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.
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: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 a = a + 1 End If NextVB 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
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 !!!!Quote:
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?
notQuote:
So may somebody know that VB has some command like 'continue' in C language, Or Not?
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?
You could always use a nasty ;) goto -- that's what Exit For, break, and continue are.
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
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...
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.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:
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).