473,248 Members | 1,427 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,248 software developers and data experts.

Exiting nested For loops

I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice workaround)?
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #1
8 7207
cut-n-pasted from the help files...Looks like you would need to nest a for
loop in a while loop and would then be limited to exiting from only one
nesting.

Skipping from Within a Nested Loop. If you have Do, For, or While loops
nested one within another, you can skip immediately to the next iteration of
any level in the nesting. This is only true, however, when the loops are of
different types. If you have nested loops of the same type, for example
nested While loops, Continue While skips to the next iteration of the
innermost While loop.
To skip to the next iteration of a Do loop from within a nested For loop
1.. Write the nested loops in the normal way.

2.. Use Continue Do at any place that you want to terminate the current
iteration of the inner For loop and skip to the next iteration of the outer
Do loop.

Copy Code
Public Sub divideElements(ByRef matrix(,) As Double)
Dim i As Integer = -1
Do Until i matrix.GetUpperBound(0)
i += 1
For j As Integer = 0 To matrix.GetUpperBound(1)
If matrix(j, j) = 0 Then Continue Do
matrix(i, j) /= matrix(j, j)
Next j
Loop
End Sub

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)?
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Nov 1 '08 #2
Try
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If Not <Boolean ExpressionThen Exit Try
<My Code>
Next
Next
Next
Catch
End Try

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #3
Hello Nathan,

I personally use labels and GoTo statements in such cases. Of course they
names are customizable.
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
Next
SkipOne:
If Not <Boolean ExpressionThen GoTo SkipTwo
Next
SkipTwo:
If Not <Boolean ExpressionThen GoTo SkipThree
Next

SkipThree:
' Continue with your code here

--
Stanimir Stoyanov
http://stoyanoff.info

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #4
Hello Nathan,

I personally use labels and GoTo statements in such cases. Of course they
names are customizable.
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
Next
SkipOne:
If Not <Boolean ExpressionThen GoTo SkipTwo
Next
SkipTwo:
If Not <Boolean ExpressionThen GoTo SkipThree
Next

SkipThree:
' Continue with your code here

--
Stanimir Stoyanov
http://stoyanoff.info

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #5
Hello Nathan,

I personally use labels and GoTo statements in such cases. Of course they
names are customizable.
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
Next
SkipOne:
If Not <Boolean ExpressionThen GoTo SkipTwo
Next
SkipTwo:
If Not <Boolean ExpressionThen GoTo SkipThree
Next

SkipThree:
' Continue with your code here

--
Stanimir Stoyanov
http://stoyanoff.info

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #6
Hello Nathan,

I personally use labels and GoTo statements in such cases. Of course they
names are customizable.
For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse GoTo SkipOne
Next
SkipOne:
If Not <Boolean ExpressionThen GoTo SkipTwo
Next
SkipTwo:
If Not <Boolean ExpressionThen GoTo SkipThree
Next

SkipThree:
' Continue with your code here

--
Stanimir Stoyanov
http://stoyanoff.info

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #7
Nathan,

Why are you using the second and thirth if, in the way you tell it, it
should never has to be reached?

Cor
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #8
I think this is more understandable to the next person to maintain your
code:

a = 0
While (a < 256 And Not quit)
b = 0
While (b < 256 And Not quit)
c = 0
While (c < 256 And Not quit)

If (anotherbool) Then
quit = True
End If

c = c + 1
End While
b = b + 1
End While
a = a + 1
End While
"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:er**************@TK2MSFTNGP03.phx.gbl...
>I have several nested For loops, as follows:

For a As Integer = 0 To 255
For b As Integer = 0 To 255
For c As Integer = 0 To 255
If <Boolean ExpressionThen <My CodeElse Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next
If Not <Boolean ExpressionThen Exit For
Next

As you can see, I need to test the condition in each For, which in my case
is probably more costly than necessary. If I could simply say exit all For
loops or exit 3 For loops or something, not only would it be less costly,
but simpler code. Is there any way to do this (or maybe a nice
workaround)? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 1 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
39
by: vineoff | last post by:
If I'm having nested loops like: for (...) for (..) for (...) { /* exit here */ } and I need to exit from there ^ . Is it better to use exceptions or goto or some other method?
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
1
by: Karl O. Pinc | last post by:
FYI, mostly. But I do have questions as to how to write code that will continue to work in subsequent postgresql versions. See code below. begintest() uses EXIT to exit a BEGIN block from...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.