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.
Printable View
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.
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
There is no continue in VB.NET 2003. So you would have to manage this with an If statement. Something like thisCode:Dim loopCounter As Long
For loopCounter = 1 To 100
'put your condition here
Next
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.
Why's that?Quote:
Originally Posted by Boumxyz2
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?Quote:
Originally Posted by Boumxyz2
- petter
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 gotoQuote:
Originally Posted by gigemboy
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
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:
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.Code:for each i as integer = 0 to limit
if not condition = true then
'do the rest here
end if
next i
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:
Or: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
I'm sure you can do this in at least 10 other ways (and not using goto)Code:dim i as integer=0
while condition and i<limit
'do the rest here
i += 1
end while
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
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:
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).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
Personally, since you have all the subroutines anyway, why not make a few boolean functions.Quote:
Originally Posted by choconlangthang
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.
u can introduce a flag that change when the third check is true
enclose the function that shd not runCode:If conditionC Then
job3()
flag=false
Code:if flag then
job22()
end if
OMG this is more complicated to read
thanCode: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
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
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.