473,657 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

break or continue out of nested loops

Hi!

Suppose I have

int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
break;
}
}
}
}

then when test comes to be true, it will break out of loop 3, increment j
and continue in loop 2. How can I break out of all three? Is there a way
to break out of say loop 3 and 2 and continue the top level loop? This
would be break(2); in php. I vaguely remember in javascript you can label
a loop to break out of a paticular one.

Is there a way to control these, structures (and others, like switch etc)
like this in C, or do I need to use temporary variables, like

int i,j,k;

for(i=0;i<I&&!b roken;++i){ /* loop 1 */
for(j=0;j<J&&!b roken;++j){ /* loop 2 */
for(k=0;k<K&&!b roken;++k){ /* loop 3 */
if(test){
broken=1;
break;
}
}
}
}

?

Thanks

Tom Viza
Nov 13 '05 #1
5 33704
viza wrote:
Is there a way to control these, structures (and others, like switch etc)
like this in C, or do I need to use temporary variables, like


Use a goto. You won't be sent to hell for it (unless you choose 'hell'
as the label. :) ).

Paul

Nov 13 '05 #2
viza wrote:

Hi!

Suppose I have

int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
break;
}
}
}
}

then when test comes to be true, it will break out of loop 3, increment j
and continue in loop 2. How can I break out of all three? Is there a way
to break out of say loop 3 and 2 and continue the top level loop? This
would be break(2); in php. I vaguely remember in javascript you can label
a loop to break out of a paticular one.

Is there a way to control these, structures (and others, like switch etc)
like this in C, or do I need to use temporary variables, like

int i,j,k;

for(i=0;i<I&&!b roken;++i){ /* loop 1 */
for(j=0;j<J&&!b roken;++j){ /* loop 2 */
for(k=0;k<K&&!b roken;++k){ /* loop 3 */
if(test){
broken=1;
break;
}
}
}
}

/* BEGIN new.c */

#include <stdio.h>

#define I 5
#define J 6
#define K 7
#define test (i == 1 && j == 2 && k ==3)

int main(void)
{
int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
goto After_nest;
}
}
}
}

After_nest:

printf("i is %d\nj is %d\nk is %d\n", i, j, k);
return 0;
}

/* END new.c */
--
pete
Nov 13 '05 #3
Ema
"viza" <no**@example.i nvalid> ha scritto nel messaggio
news:ha******** *********@newsf ep2-gui.server.ntli .net...
Hi!

Suppose I have

int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
break;
}
}
}
}


Don't know if you will like it: it's not elegant but it should run.

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test) break;
}
if (test) break;
}
}

Bye,
Ema
Nov 13 '05 #4


viza wrote:

Hi!

Suppose I have

int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
break;
}
}
}
}

then when test comes to be true, it will break out of loop 3, increment j
and continue in loop 2. How can I break out of all three?
void Operate()
{
int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
return;
}
}
}
}
}

Is there a way
to break out of say loop 3 and 2 and continue the top level loop?


void OperateInner()
{
int j,k;

for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
return;
}
}
}
}

void Operate()
{
int i;

for(i=0;i<I;++i ){ /* loop 1 */
OperateInner();
}

--
Karl Heinz Buchegger
kb******@gascad .at
Nov 13 '05 #5
viza <no**@example.i nvalid> wrote in message news:<ha******* **********@news fep2-gui.server.ntli .net>...
Hi!
One more idea. May be this is not exactly what you are looking for
(but without using gotos/multiple functions/temporary variables)

for(i=0;i<10;i+ +)
{
for(j=0;j<10;j+ +)
{
for(k=0;k<10;k+ +)
{
if(i== 6 && j== 5 && k == 5) /*Complelte
condition*/
break;
}
if(k == 5) /*Test if we executed break from k
loop*/
break;
}
if(j == 5) /*Text if we executed break from j loop*/
break;
}
Regards,
Sunil.
Hi!

Suppose I have

int i,j,k;

for(i=0;i<I;++i ){ /* loop 1 */
for(j=0;j<J;++j ){ /* loop 2 */
for(k=0;k<K;++k ){ /* loop 3 */
if(test){
break;
}
}
}
}

then when test comes to be true, it will break out of loop 3, increment j
and continue in loop 2. How can I break out of all three? Is there a way
to break out of say loop 3 and 2 and continue the top level loop? This
would be break(2); in php. I vaguely remember in javascript you can label
a loop to break out of a paticular one.

Is there a way to control these, structures (and others, like switch etc)
like this in C, or do I need to use temporary variables, like

int i,j,k;

for(i=0;i<I&&!b roken;++i){ /* loop 1 */
for(j=0;j<J&&!b roken;++j){ /* loop 2 */
for(k=0;k<K&&!b roken;++k){ /* loop 3 */
if(test){
broken=1;
break;
}
}
}
}

?

Thanks

Tom Viza

Nov 13 '05 #6

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

Similar topics

3
5109
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if we need to break once more... So maybe there should be an argument to break that is an int which is a number of loops to break. So it would default to 1 (or whatever means the current enclosing loop), not breaking any code...
5
2534
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
0
1784
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)
46
9911
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?...
9
1867
by: cody | last post by:
for (int serie=1; serie < TurnierGruppe.AktuelleSerie.Serie; serie++) { //outer: here is works foreach (Tischbesetzung tb in TurnierGruppe.Serien.Tischebesetzungen) { for (int i=0; i<tb.SpielerAnzahl; i++) { if (tb.Spieler.Spieler==this) {
5
2522
by: Uday Deo | last post by:
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
77
5195
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?
12
1454
by: Rich Shepard | last post by:
I want to code what would be nested "for" loops in C, but I don't know the most elegant way of doing the same thing in python. So I need to learn how from you folks. Here's what I need to do: build a database table of 180 rows. Each row contains 31 columns: the first is an automatically incremented integer as the primary key; the next two fields can each contain one of three strings held in dictionaries, the last 28 fields are random...
0
8297
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
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8498
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
7311
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
6162
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
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1930
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.