break or continue out of nested loops

V

viza

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&&!broken;++i){ /* loop 1 */
for(j=0;j<J&&!broken;++j){ /* loop 2 */
for(k=0;k<K&&!broken;++k){ /* loop 3 */
if(test){
broken=1;
break;
}
}
}
}

?

Thanks

Tom Viza
 
P

Paul F. Dietz

viza said:
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
 
P

pete

viza said:
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&&!broken;++i){ /* loop 1 */
for(j=0;j<J&&!broken;++j){ /* loop 2 */
for(k=0;k<K&&!broken;++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 */
 
E

Ema

viza said:
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
 
K

Karl Heinz Buchegger

viza said:
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();
}
 
S

sunil

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top