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

nested for loops

Hi,

Once again I have a question :

In my app, I have nested loops, very basic stuff, like

for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
if (expression)
{
do something
}
else
{
do something else
}
}
}

What I would like to do, is when I reach "do something else" move on to the
next step of the outer loop.
So i = 2 ; j = 24
I encounter "do something else"
now I want to move on to i = 3

How can I accomplish this?

Thanks
Nov 15 '05 #1
5 11212
Ok, got that wrong, I actually have 3 nested loops and when I encounter "do
something else" in the innermost loop, I want the outermost loop to move on
to the next step.

Thanks.
"dawn" <da******@hotmail.com> schreef in bericht
news:9O**********************@phobos.telenet-ops.be...
Hi,

Once again I have a question :

In my app, I have nested loops, very basic stuff, like

for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
if (expression)
{
do something
}
else
{
do something else
}
}
}

What I would like to do, is when I reach "do something else" move on to the next step of the outer loop.
So i = 2 ; j = 24
I encounter "do something else"
now I want to move on to i = 3

How can I accomplish this?

Thanks

Nov 15 '05 #2
Hm, to step out of one loop you could simply use break, but to step out of
the parent loop too I think you need to set a variable, something like
bool stepOut;

bool stepOut = false;

for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
for ( int k = 0; k < 100; k++)
{
if (expression)
{
do something
}
else
{
do something else
stepOut = true;
break;
}
}
if(stepOut)
{
stepOut = false;
break;
}
}
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
Morten Wennevik wrote:
Hm, to step out of one loop you could simply use break, but to step out
of the parent loop too I think you need to set a variable, something
like bool stepOut;

bool stepOut = false;

for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
for ( int k = 0; k < 100; k++)
{
if (expression)
{
do something
}
else
{
do something else
stepOut = true;
break;
}
}
if(stepOut)
{
stepOut = false;
break;
}
}
}


Either that or use a goto. (horrible practice, btw)
outer_loop:
for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
if (expression)
{
do something
}
else
{
goto outer_loop;
}
}
}

Nov 15 '05 #4
There are three cases, I don't know what you really want to do:
1. increment the outer loop, restart immediately the inner loop: use "break"
( from i=4, j=23 you'll have i=5,j=0 and the execution jumps to the
beggining of the inner loop)

2. increment the outer loop, reset the innner loop without restarting the
inner loop: use "i++; j=-1" ( from i=4, j=23 you'll have i=5,j=-1 the inner
loop continues with j=-1!!!; next on inner loop will be i=5; j=0)

3. increment the outer loop whitout restarting/resetting the inner loop:
"i++" ( from i=4, j=23 you'll have i=5,j=23 with continuation of the inner
loop; next on inner loop will be i=5; j=24)

--
Horatiu Ripa
"dawn" <da******@hotmail.com> wrote in message
news:9O**********************@phobos.telenet-ops.be...
Hi,

Once again I have a question :

In my app, I have nested loops, very basic stuff, like

for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
if (expression)
{
do something
}
else
{
do something else
}
}
}

What I would like to do, is when I reach "do something else" move on to the next step of the outer loop.
So i = 2 ; j = 24
I encounter "do something else"
now I want to move on to i = 3

How can I accomplish this?

Thanks

Nov 15 '05 #5
The way to do this "by the book", would be to use a while-loop, wouldn't it?
Like:
int i = 0;
int j;
int k;
bool BreakToOuter = false;
while (i<50)
{
j = 0;
while ((j < 75) && !BreakToOuter)
{
k=0;
while ((k < 100) && !BreakToOuter)
{
if (expression)
{
//Do something
}
else
{
//Do something else
BreakToOuter = true;
}
++k;
}
++j;
}
++i;
BreakToOuter = False;
}

No "GOTO"-style behaviour, but you buy that by some more code.

Scarfeet

"dawn" <da******@hotmail.com> schrieb im Newsbeitrag
news:_P**********************@phobos.telenet-ops.be...
Ok, got that wrong, I actually have 3 nested loops and when I encounter "do something else" in the innermost loop, I want the outermost loop to move on to the next step.

Thanks.
"dawn" <da******@hotmail.com> schreef in bericht
news:9O**********************@phobos.telenet-ops.be...
Hi,

Once again I have a question :

In my app, I have nested loops, very basic stuff, like

for (int i = 0; i < 50; i++)
{
for (int j = 0 ; j < 75; j++)
{
if (expression)
{
do something
}
else
{
do something else
}
}
}

What I would like to do, is when I reach "do something else" move on to

the
next step of the outer loop.
So i = 2 ; j = 24
I encounter "do something else"
now I want to move on to i = 3

How can I accomplish this?

Thanks


Nov 15 '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...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
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...
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...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.