473,396 Members | 1,756 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.

Breaking on certain conditions in nested loops

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

Nov 21 '05 #1
5 2489
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
"Uday Deo" <Ud*****@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
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

Nov 21 '05 #2
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

"Will Gillen" wrote:
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
"Uday Deo" <Ud*****@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
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


Nov 21 '05 #3
Uday,

Not in this version, unlucky you can in the 2005 version.

Cor
Nov 21 '05 #4
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

"Uday Deo" <Ud*****@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
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

"Will Gillen" wrote:
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
"Uday Deo" <Ud*****@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
> 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
>
>
>


Nov 21 '05 #5
"Uday Deo" <Ud*****@discussions.microsoft.com> schrieb:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #6

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...
5
by: Ray Gibbon | last post by:
Testing conditions. Common scenario. Old programmer, new to Python, love it, but still hankering after some of my old ways. Of all of it's 'new to me' features, I appear to be enjoying 'no...
3
by: Dennis M. Marks | last post by:
I have a 3 level array. First level is a list of trains. Second level are items about the train. Third level is where there are multiples of the second level item. The search will be of myArray...
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...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
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...
37
by: Prisoner at War | last post by:
Actually, it doesn't have to be a blockquote...but I'm at my wits' end: I want to make bold several lines of text which have a pair of <br /tags between them...seems like the <b></bdo not "carry...
8
by: Nathan Sokalski | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.