Double break

  • Thread starter those who know me have no need of my name
  • Start date
T

those who know me have no need of my name

in comp.lang.c i read:
Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

nope.
 
R

Richard Tobin

Martin Johansen said:
Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

Why do you care, since you can use a goto?

-- Richard
 
T

Trevor Fancher

Martin said:
Hello group.

Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

e.g.

for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
z;
break(2); /* i know the 2 does not help */
}

K&R suggests using goto in section 3.8 . They say it is about the only
time it is okay to use goto.

-Trevor
 
K

kal

Martin Johansen said:
Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

e.g.

for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
z;
break(2); /* i know the 2 does not help */
}

No.

Sometimes you can use the inner loop's condition immediately
after that loop. e.g. "If j < y break;"

<OT>
Which is preferable: "are there any efficient way", "Are there
any efficient ways", "Is there any efficient way", "Is there
any efficient ways" or "Is there an efficient way?"
</OT>
 
C

CBFalconer

Martin said:
Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

e.g.

for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
z;
break(2); /* i know the 2 does not help */
}

Well, the most efficient code for that snippet is:

i = 0; j = 0; z;

assuming x and y both >= 0. <g,d,&r>

(We can work out the appropriate actions for other x and y)
 
M

Martin Johansen

Hello group.

Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

e.g.

for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
z;
break(2); /* i know the 2 does not help */
}
 
Y

Yu SONG

Trevor said:
Martin Johansen wrote:




K&R suggests using goto in section 3.8 . They say it is about the only
time it is okay to use goto.

-Trevor

Or you can put them in a function,

Replace the "break(2)" by a "return ABC" statement;


--
Song

/* E-mail.c */
#define User "Y.Song"
#define Warwick "dcs.warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s@%s", User, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 
D

Darrell Grainger

Hello group.

Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

By 'using a jump' go you mean using a goto? This is the most efficient way
of exiting nested loops. Even using a flag (extra variable) or modifying i
and j will not be as efficient as using a goto.

Additionally, you might want to retain the last value of i and j when you
exit the loops. A goto will accomplish this.
 
T

Thomas Matthews

Yu said:
Or you can put them in a function,

Replace the "break(2)" by a "return ABC" statement;

The problem is that a return will exit the function.
If there is more processing after the for loops, your
solution will not allow the execution of stuff after
the for loops.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Mike Wahler

Martin Johansen said:
Hello group.

Are there any efficient

Define 'efficient'.
way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

e.g.

for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
z;
break(2); /* i know the 2 does not help */
}

The *only* way that meets your above constraints is
to modify 'i' (set it greater or equal to 'y') during
the inner loop. Had you required 'without modifying i or j',
then afaik, there's no way at all.

-Mike
 
A

Alan Balmer

No.

Sometimes you can use the inner loop's condition immediately
after that loop. e.g. "If j < y break;"

<OT>
Which is preferable:

(1) "are there any efficient way",
(2) "Are there any efficient ways",
(3) "Is there any efficient way",
(4) "Is there any efficient ways" or
(5) "Is there an efficient way?"

Choices 2, 3 and 5 are all grammatically correct. Choices 3 and 5 have
essentially the same meaning, and I would not have a preference.
Choice 2 differs only in that it allows for multiple efficient ways.
 
R

Richard Tobin

Why do you care, since you can use a goto?
[/QUOTE]

So what's your point? An explicit break construct would be slightly
nicer for readability, but it needs to identify which loop it's
breaking from, so it needs some kind of label. (Having to count how
many loops to break from would be like a goto where you have to say
how many lines to skip.) If you use a sensible label ("a_loop_end"
for example) a goto conveys the intention quite clearly.

-- Richard
 
N

Neil Kurzman

Martin said:
Hello group.

Are there any efficient way to double 'break' a 2-dimensional loop
without using a jump, an extra variable or midifying i and j?

e.g.

for(i = 0; i < x; i++)
for(j = 0; j < y; j++){
z;
break(2); /* i know the 2 does not help */
}

how about?

done =0;
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
done =1;
break;
}
}
if(done)break;
}

My first teacher said gotos are evil, and if I used one I would be
condemned to an infinite loop.
 
Y

Yu SONG

Thomas said:
The problem is that a return will exit the function.
If there is more processing after the for loops, your
solution will not allow the execution of stuff after
the for loops.

The code after the "for" loops can be placed in the routine where this
function is called. See this simple program below,

int forloop() {
for ...
for ...
return 1;
return 0;
}

int main() {
if (forloop() == 0)
/* processing for expected output */
else
/* processing for unexpected output */
}



--
Song

/* E-mail.c */
#define User "Y.Song"
#define Warwick "dcs.warwick.ac.uk"
int main() {
printf("Yu Song's E-mail: %s@%s", User, Warwick);
return 0;}

Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
_______________________________________________________
 
X

Xenos

Neil Kurzman said:
how about?

done =0;
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
done =1;
break;
}
}
if(done)break;
}

My first teacher said gotos are evil, and if I used one I would be
condemned to an infinite loop.
I have never understood why people think that this is any better or more
readable than:

for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
goto end_outer_loop;
}
}
}
end_outer_loop:

I would argue that the logic with the goto is cleaner. gotos aren't bad.
It's all in how they are used.

DrX
 
A

Alan Balmer

I have never understood why people think that this is any better or more
readable than:

for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
goto end_outer_loop;
}
}
}
end_outer_loop:
Because the former is irrevocably tied to the control structure. The
latter is not. Certainly not a serious problem, in this simple case,
but it means that maintenance programmers have to be just a bit more
careful.
 
G

Guest

Xenos said:
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
goto end_outer_loop;
}
}
}
end_outer_loop:

In Java (here Java is OT... I known!):

outerloop: for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
break outer_loop;
}
}
}

- Dario
 
X

Xenos

Dario (drinking coï¬?ee in the oï¬fceâ?¦) said:
In Java (here Java is OT... I known!):

outerloop: for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
z;
if(?????)
{
break outer_loop;
}
}
}

- Dario

Yes, I know, same as Ada's exit statement. So what?
 
X

Xenos

Alan Balmer said:
Because the former is irrevocably tied to the control structure. The
latter is not. Certainly not a serious problem, in this simple case,
but it means that maintenance programmers have to be just a bit more
careful.
I disagree that the former method has anything to do with the control
structure, but I won't argue the point. I will simply ask if you would use
this same method for an Nth dimensional loop, where N is any positive
integer. Sure, nested loops 3 or more deep are rare (and probably in most
cases bad), but would you really argue the "if (flag) break;" mechanism is
better to break out of several levels of scope, simply to avoid a single
goto?

DrX
 

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

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top