By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,874 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,874 IT Pros & Developers. It's quick & easy.

Next iteration in a for loop?

P: n/a

Is there any way to "skip" iterations in a for loop?

Example :

for x = 1 to 10

if something = 1
next
endif

next

I see the Exit For, but I just need to skip iterations sometimes, not
jump out all together.

Thanks!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
For x = 1 To 10
If something <> 1 Then
...
End If
Next
"Mitchell Vincent" <mv******@newsgroup.nospam> wrote in message
news:uX*************@tk2msftngp13.phx.gbl...

Is there any way to "skip" iterations in a for loop?

Example :

for x = 1 to 10

if something = 1
next
endif

next

I see the Exit For, but I just need to skip iterations sometimes, not jump
out all together.

Thanks!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com

Nov 21 '05 #2

P: n/a
There is no equivalent of 'continue' in VB. Maybe 2.0 has it, don't know...

"Mitchell Vincent" <mv******@newsgroup.nospam> wrote in message
news:uX*************@tk2msftngp13.phx.gbl...

Is there any way to "skip" iterations in a for loop?

Example :

for x = 1 to 10

if something = 1
next
endif

next

I see the Exit For, but I just need to skip iterations sometimes, not jump
out all together.

Thanks!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com

Nov 21 '05 #3

P: n/a
Marina wrote:
There is no equivalent of 'continue' in VB. Maybe 2.0 has it, don't know...


I just looked it up and 2.0 *does* have continue - woohoo!
--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #4

P: n/a
Version 2.0 has it, but the same underlying logic is available via (gasp)
goto's. I hate goto's as much as the next person, but that *is* what
"Continue For" is in version 2.0. Continue's and exit's (or 'break' in C#)
are all just specialized goto's that do not require a label.
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#
"Mitchell Vincent" wrote:

Is there any way to "skip" iterations in a for loop?

Example :

for x = 1 to 10

if something = 1
next
endif

next

I see the Exit For, but I just need to skip iterations sometimes, not
jump out all together.

Thanks!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com

Nov 21 '05 #5

P: n/a
"Mitchell Vincent" <mv******@newsgroup.nospam> schrieb:
Is there any way to "skip" iterations in a for loop?

Example :

for x = 1 to 10

if something = 1
next
endif

next

I see the Exit For, but I just need to skip iterations sometimes, not jump
out all together.


VB 2005 will provide 'Continue' and 'Continue <type of loop>' statements:

<URL:http://groups.google.to/group/microsoft.public.dotnet.languages.vb/msg/abe04dd3937bab63>

Note that in many cases it's easy to replace 'Continue' by a more structured
'If...Then...Else...' block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6

P: n/a
In article <OV**************@TK2MSFTNGP10.phx.gbl>, Herfried K. Wagner [MVP] wrote:
"Mitchell Vincent" <mv******@newsgroup.nospam> schrieb:
Is there any way to "skip" iterations in a for loop?

Example :

for x = 1 to 10

if something = 1
next
endif

next

I see the Exit For, but I just need to skip iterations sometimes, not jump
out all together.


VB 2005 will provide 'Continue' and 'Continue <type of loop>' statements:

<URL:http://groups.google.to/group/microsoft.public.dotnet.languages.vb/msg/abe04dd3937bab63>

Note that in many cases it's easy to replace 'Continue' by a more structured
'If...Then...Else...' block.


This is one of the places that I find Goto acceptable. Sometimes. If
you start getting into deeply nested if...then statements, sometimes the
goto can be a little cleaner.

I'm glad they are finally introducint the Continue keyword though, then
I won't feel so bad about it :)

--
Tom Shelton [MVP]
Nov 21 '05 #7

P: n/a
Tom,

I see not any need for a continu or a goto, the sample question askes, is
it possible to skip sometimes a loop in the subject next iteration in a for
loop.

As alternative for Herfrieds 'if else" answer what I agree with, however
that can be complex in a loop.

Do while whatever
select case argument
case 1, 2, 4, 7
case else
'do what not has to be skipped
end select
Next

This the same for a for each loop, a do loop or a for index loop.

Just my thought,

Cor
Nov 21 '05 #8

P: n/a
Cor, I have to disagree with you there. When it's a simple case, then OK,
you can deal with an if or a case statement.

However, many times, the blocks of code in a loop can be quite complex, and
the 'continue' would be burried deeply in there, and it would be very
difficult to structure a way to skip the rest of the loop. Because it's not
always an all or nothing kind of thing - you may need to execute some of the
beginning parts - but not later. And with all the nesting that can occur,
it is difficult to get out of that iteration of the loop.

I think goto's can be avoided far more easily, since the code can be
restructured to have more methods, etc. But I think continue is quite
useful.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Tom,

I see not any need for a continu or a goto, the sample question askes, is
it possible to skip sometimes a loop in the subject next iteration in a
for loop.

As alternative for Herfrieds 'if else" answer what I agree with, however
that can be complex in a loop.

Do while whatever
select case argument
case 1, 2, 4, 7
case else
'do what not has to be skipped
end select
Next

This the same for a for each loop, a do loop or a for index loop.

Just my thought,

Cor

Nov 21 '05 #9

P: n/a

"Marina" <so*****@nospam.com> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
Cor, I have to disagree with you there. When it's a simple case, then OK,
you can deal with an if or a case statement.

However, many times, the blocks of code in a loop can be quite complex,
and the 'continue' would be burried deeply in there, and it would be very
difficult to structure a way to skip the rest of the loop. Because it's
not always an all or nothing kind of thing - you may need to execute some
of the beginning parts - but not later. And with all the nesting that can
occur, it is difficult to get out of that iteration of the loop.

I think goto's can be avoided far more easily, since the code can be
restructured to have more methods, etc. But I think continue is quite
useful.


You can do it without goto's .. remember, there are many ways to code the
same piece of functionality. Give us an example of something that is so
complex'ed with if's, and we (me for one) will write it showing you how to
do it without goto's or continue's. When a continue exists for a language I
am writing in, great, I use it. If it doesn't, it really doesn't slow me
down much. Just the state of mind I'm in while developing, and using the
tools I have available.

Anyways, if the if's are so complex that you can't seem to figure out how to
do it without a goto or some sort, then you may need to rethink the approach
you are taking, maybe the approach you are taking is too complex and can be
simplified?

HTH,
Mythran

Nov 21 '05 #10

P: n/a
Marina,

Give us the challenge Mythran gave and probably we both will take it

By the way I am busy with a sample for that MSHTM, I see now it is quit
easy.

Still interested?

(Completely get a page from internet and get the innertext).

:-)

Cor

Nov 21 '05 #11

P: n/a
I didn't say it cannot be done. I said, the work involved is not worth the
inconvenience of having to use 'continue'. And it would probably involve
extra variables, and extra checks. Example:

While (someCondition)
If var1=1 Then
DoThisFirst()

If var2 = 2 Then
DoStuff()
ElseIf var3 = 5 Then
Continue
End If

DoSomeMoreStuff()
Else
DoThisOtherThing()
End If

SomeMoreCodeHere()
End While

Now, you can rewrite it of course, but it will be a lot less easy to
understand exactly what is going on.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
Marina,

Give us the challenge Mythran gave and probably we both will take it

By the way I am busy with a sample for that MSHTM, I see now it is quit
easy.

Still interested?

(Completely get a page from internet and get the innertext).

:-)

Cor

Nov 21 '05 #12

P: n/a
Cor Ligthert [MVP] wrote:
Tom,

I see not any need for a continu or a goto, the sample question askes, is
it possible to skip sometimes a loop in the subject next iteration in a for
loop.


I'm not sure who said it but "A goo programming language should make
easy things easy and hard things possible" - continue should be there
(regardless of how it is implemented) for those cases, rare as they may
be, that nothing else will do.

Yes you can get around using them, but I'd rather spend as little time
possible working around language constructs that aren't there!

Thanks Cor, I appreciate your seemingly infinte insight into all things
..NET! :-)

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #13

P: n/a
In article <uO**************@TK2MSFTNGP10.phx.gbl>, Mythran wrote:

"Marina" <so*****@nospam.com> wrote in message
news:ux**************@TK2MSFTNGP15.phx.gbl...
Cor, I have to disagree with you there. When it's a simple case, then OK,
you can deal with an if or a case statement.

However, many times, the blocks of code in a loop can be quite complex,
and the 'continue' would be burried deeply in there, and it would be very
difficult to structure a way to skip the rest of the loop. Because it's
not always an all or nothing kind of thing - you may need to execute some
of the beginning parts - but not later. And with all the nesting that can
occur, it is difficult to get out of that iteration of the loop.

I think goto's can be avoided far more easily, since the code can be
restructured to have more methods, etc. But I think continue is quite
useful.


You can do it without goto's .. remember, there are many ways to code the
same piece of functionality. Give us an example of something that is so
complex'ed with if's, and we (me for one) will write it showing you how to
do it without goto's or continue's. When a continue exists for a language I
am writing in, great, I use it. If it doesn't, it really doesn't slow me
down much. Just the state of mind I'm in while developing, and using the
tools I have available.

Anyways, if the if's are so complex that you can't seem to figure out how to
do it without a goto or some sort, then you may need to rethink the approach
you are taking, maybe the approach you are taking is too complex and can be
simplified?


There are always ways around goto. No one is arguing that (at least, I
don't think anyone is :). But, it has to be asked - is it worth it?

I've been through the computer sci indoctination myself - and I have a
general distaste for Goto. But, on a rare occasion using a goto can
simplify code. Anyway, this is a topic that brings out a sort of
"religious" extreamism in most programmers, so is probably counter
productive in the long term :)

--
Tom Shelton [MVP]
Nov 21 '05 #14

P: n/a
In article <#M*************@TK2MSFTNGP11.phx.gbl>, Cor Ligthert [MVP] wrote:
Tom,

I see not any need for a continu or a goto, the sample question askes, is
it possible to skip sometimes a loop in the subject next iteration in a for
loop.

As alternative for Herfrieds 'if else" answer what I agree with, however
that can be complex in a loop.

Do while whatever
select case argument
case 1, 2, 4, 7
case else
'do what not has to be skipped
end select
Next

This the same for a for each loop, a do loop or a for index loop.

Just my thought,

Cor


Cor,

I am certainly not stating that continue (or goto) are ever needed, they
aren't. There is always a way to write code in such a way as to make
them unnecessary - but, there are times when having these types of tools
makes the code a little cleaner. And believe me, if I will always use a
continue over a goto :)

--
Tom Shelton [MVP]
Nov 21 '05 #15

P: n/a
I've been programming (both as hobby and at times professionally) before most
of you were born and I find GoTo's very useful in some situations...beats me
why one would type several characters of code when a few would do nicely just
to maintain their "programming religion".. Might want to try Assembler
programming without GoTo's (Jumps as they are sometimes called).
--
Dennis in Houston
"Tom Shelton" wrote:
In article <#M*************@TK2MSFTNGP11.phx.gbl>, Cor Ligthert [MVP] wrote:
Tom,

I see not any need for a continu or a goto, the sample question askes, is
it possible to skip sometimes a loop in the subject next iteration in a for
loop.

As alternative for Herfrieds 'if else" answer what I agree with, however
that can be complex in a loop.

Do while whatever
select case argument
case 1, 2, 4, 7
case else
'do what not has to be skipped
end select
Next

This the same for a for each loop, a do loop or a for index loop.

Just my thought,

Cor


Cor,

I am certainly not stating that continue (or goto) are ever needed, they
aren't. There is always a way to write code in such a way as to make
them unnecessary - but, there are times when having these types of tools
makes the code a little cleaner. And believe me, if I will always use a
continue over a goto :)

--
Tom Shelton [MVP]

Nov 21 '05 #16

P: n/a
Dennis,

C is already an older language as Cobol is. It started at the time Goto Less
programming became a topic.

http://www.acm.org/classics/oct95/

Therefore there are in that language old elements as we see them in
assembler languages (Branch conditional and unconditional).

That should not mean that they should not be avoided. In Cobol is an "Alter"
statement which has in fact the same effect as changing an address in a
branch statement. No Cobol programmer would use that statement after the
90's

However some C programmers see this as a religion and want to keep there old
C style programming in all from C derived languages.

That should in my opinion not mean that VB programmers should do that as
well.

AFAIK is Pascal created as a counterpart for this old style C programmers.

Just my opinion,

Cor.

Nov 21 '05 #17

P: n/a
Marina,

In the sample I was making for mshtml is almost the same code as you
describe (the last part). Do you mind that I show you that instead of doing
that with your sample.

If you want I will make it with your code as well.

http://www.windowsformsdatagridhelp....f-56dbb63fdf1c

I hope it gives some extra's as well.

Cor

"Marina" <so*****@nospam.com> schreef in bericht
news:Oc**************@TK2MSFTNGP15.phx.gbl...
I didn't say it cannot be done. I said, the work involved is not worth the
inconvenience of having to use 'continue'. And it would probably involve
extra variables, and extra checks. Example:

While (someCondition)
If var1=1 Then
DoThisFirst()

If var2 = 2 Then
DoStuff()
ElseIf var3 = 5 Then
Continue
End If

DoSomeMoreStuff()
Else
DoThisOtherThing()
End If

SomeMoreCodeHere()
End While

Now, you can rewrite it of course, but it will be a lot less easy to
understand exactly what is going on.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
Marina,

Give us the challenge Mythran gave and probably we both will take it

By the way I am busy with a sample for that MSHTM, I see now it is quit
easy.

Still interested?

(Completely get a page from internet and get the innertext).

:-)

Cor


Nov 21 '05 #18

P: n/a
Huh? What are you talking about?

That was an example of code that would be difficult and error prone to
rewrite without a 'continue'.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eV****************@TK2MSFTNGP12.phx.gbl...
Marina,

In the sample I was making for mshtml is almost the same code as you
describe (the last part). Do you mind that I show you that instead of
doing that with your sample.

If you want I will make it with your code as well.

http://www.windowsformsdatagridhelp....f-56dbb63fdf1c

I hope it gives some extra's as well.

Cor

"Marina" <so*****@nospam.com> schreef in bericht
news:Oc**************@TK2MSFTNGP15.phx.gbl...
I didn't say it cannot be done. I said, the work involved is not worth
the inconvenience of having to use 'continue'. And it would probably
involve extra variables, and extra checks. Example:

While (someCondition)
If var1=1 Then
DoThisFirst()

If var2 = 2 Then
DoStuff()
ElseIf var3 = 5 Then
Continue
End If

DoSomeMoreStuff()
Else
DoThisOtherThing()
End If

SomeMoreCodeHere()
End While

Now, you can rewrite it of course, but it will be a lot less easy to
understand exactly what is going on.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
Marina,

Give us the challenge Mythran gave and probably we both will take it

By the way I am busy with a sample for that MSHTM, I see now it is quit
easy.

Still interested?

(Completely get a page from internet and get the innertext).

:-)

Cor



Nov 21 '05 #19

P: n/a
Marina,

Reading it over, as always, you are right,

:-)

Therefore see bellow.

Probably this is not the one you want.

\\\See the real one behind this
While condition
While 1 = 1 'faking
If var1 = 1 Then
Dim v1 As Integer = 1 + 1
If var2 = 2 Then
Dim v2 As Integer = 1 + 1
ElseIf var3 = 5 Then
Exit While
End If
Dim v3 As Integer = 1 + 1
Else
Dim v4 As Integer = 1 + 1
End If
Dim v As Integer = 1 + 1
End While
End While
///

\\\ However I find this more readable
While condition
If var1 = 1 Then
Dim v1 As Integer = 1 + 1
If var2 = 2 Then
Dim v2 As Integer = 1 + 1
ElseIf var3 <> 5 Then
Dim v3 As Integer = 1 + 1
End If
Else
Dim v4 As Integer = 1 + 1
End If
If var3 <> 5 Then
Dim v As Integer = 1 + 1
End If
End While
///

I have not really tested the last one.
However the point is the extra condition which tell when it should be done.

Cor
Nov 21 '05 #20

P: n/a
Sorry, still not following. That wasn't rewriting the loop I had to be
equivalent and with no continue.

The first example had an 'Exit While'. but that is not the same as having a
'Continue' there. And I'm not really sure what the second one was supposed
to be doing...

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Marina,

Reading it over, as always, you are right,

:-)

Therefore see bellow.

Probably this is not the one you want.

\\\See the real one behind this
While condition
While 1 = 1 'faking
If var1 = 1 Then
Dim v1 As Integer = 1 + 1
If var2 = 2 Then
Dim v2 As Integer = 1 + 1
ElseIf var3 = 5 Then
Exit While
End If
Dim v3 As Integer = 1 + 1
Else
Dim v4 As Integer = 1 + 1
End If
Dim v As Integer = 1 + 1
End While
End While
///

\\\ However I find this more readable
While condition
If var1 = 1 Then
Dim v1 As Integer = 1 + 1
If var2 = 2 Then
Dim v2 As Integer = 1 + 1
ElseIf var3 <> 5 Then
Dim v3 As Integer = 1 + 1
End If
Else
Dim v4 As Integer = 1 + 1
End If
If var3 <> 5 Then
Dim v As Integer = 1 + 1
End If
End While
///

I have not really tested the last one.
However the point is the extra condition which tell when it should be
done.

Cor

Nov 21 '05 #21

P: n/a
Dennis wrote:
I've been programming (both as hobby and at times professionally) before most
of you were born and I find GoTo's very useful in some situations...beats me
why one would type several characters of code when a few would do nicely just
to maintain their "programming religion".. Might want to try Assembler
programming without GoTo's (Jumps as they are sometimes called).


It's always "the best tool for the job" for me. Sometimes that is a
GoTo, sometimes it isn't!

I think the religion surrounding GoTo started because it was being
overused and misused, kind of funny how that happens!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #22

P: n/a
Marina,

It does not exit the while it restart the outerloop and is only exiting the
innerloop which is endless.

The second sample shows without that restarting the loop a nicer
documentative use.

Cor
Nov 21 '05 #23

P: n/a
Hi

So I think this is also an alternative to workaround the problem besides If
statement.
Also in vb.net 2005, there will be a new keyword Continue added.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.