Breaking on certain conditions in nested loops

G

Guest

Hi everyone,

I am looping through 4 nested loops and I would like to break in the inner
most loop on certain condition and get the control on the 2 nd loop instead
of 3rd loop.
Here is briefly what I am trying to accomplish.

//Loop 1
For each lp_1 in Loop1
// Certain conditions
// Loop 2
For each lp_2 in Loop2
// Certain conditions
//Loop 3
For each lp_3 in Loop3
// Certain conditions
//Loop 4
For each lp_4 in Loop4
// certain condition
exit for loop 4 and also exit for loop 3 so that
control would move
loop2.

Please ignore the syntax. I would like to know if we can jump from inner
most loop to the intermediate (not immediate parnet loop) loop using either
VB.NET or C#.

Any ideas/ thoughts??

Thanks,
Uday
 
W

Will Gillen

How about using a boolean switch to determine if you should ALSO exit loop
3:

Dim bExitLoop3 as boolean

//Loop 1
For each lp_1 in Loop1
// Certain conditions
// Loop 2
For each lp_2 in Loop2
// Certain conditions
//Loop 3
bExitLoop3 = false
For each lp_3 in Loop3
if bExitLoop3 then Exit Loop
// Certain conditions
//Loop 4
For each lp_4 in Loop4
// certain condition
bExitLoop3 = True
Exit Loop
Next //Loop 4
Next //Loop 3
Next //Loop2
Next //Loop1
 
G

Guest

Thanks!
This will definately help to resolve the particular situation.

I am up against a Java developer and he is giving me hard time about the
limitations. :)

So does anyone know if there is inbuilt functionality?
Is there something like labelling the for loops and then providing break /
continue commands for the label name.

- Uday
 
J

Jay B. Harlow [MVP - Outlook]

Uday,
You can use an Exit For to break out of a For Loop.

You will need to wait until VS.NET 2005 (currently in beta, due out later in
2005) for a Continue statement.

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

Uday Deo said:
So does anyone know if there is inbuilt functionality?
Is there something like labelling the for loops and then
providing break / continue commands for the label name.

You can add a label to the destination line and use 'GoTo' to go to the
label.

\\\
Foo: i = 10
..
..
..
GoTo Foo
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top