473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Next iteration in a for loop?


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
23 21965
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
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
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
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
"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
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
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
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

"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DottingTheNet | last post by:
Hi ppl!! just a newbie ques is there something in PL/SQL that skips to the next iteration in the for loop like a next, etc.???? Thnx!!
4
by: Christopher Benson-Manica | last post by:
I have a situation something like this: int foo; for( foo=bar() ; foo <= bar()+1 ; foo++ ) { if( !baz(foo) ) break; /* do stuff with foo */ } The idea is that the loop happens at most...
2
by: Marc Miller | last post by:
Is there any way in VB to force the next iteration in a For ... next loop other than using a GoTo? I notice that in C++ you can use 'continue' and in other languages there are syntax for this. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.