473,803 Members | 3,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11230
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******@hotma il.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******@hotma il.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******@hotma il.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******@hotma il.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
12716
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 increase. Does Python have a limit on the number of nested for-loops? Thanks.
0
1793
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 dict() print combo2(5)
4
7044
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 what's the alternative? Would a group-by clause in the SELECT do the trick? Right now we're doing: (pseudo-code)--------------------------------- For each team print team name For each peson print person name
46
9944
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 already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a solution? i.e. what are nested loops useful for? What kinds of algorithms are served by nested loops?...
77
5253
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. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler?
9
2857
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
3128
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 trying to understand Goto in this contect. PS: This is not homework.
4
2340
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 way I could write this to get both loops to complete fully? I am using the two while loops to pull data from different tables, and insert that data into a list that has html code surrounding each loop. while ($url = mysql_fetch_array($urls,...
13
2706
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
7267
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 <Boolean ExpressionThen Exit For Next If Not <Boolean ExpressionThen Exit For
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.