More loop style stuff

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I have a situation something like this:

int b, i, j;

for( i=0 ; i < 7 ; i++ ) {
b=0;
for( j=0 ; j < 8 ; j++ ) {
if( some_func() || other_func(i,j) ) {
b=1;
break;
}
}
if( !b ) {
continue;
}
/* do various other things */
}

I'm quite certain this is a bad way to do this, but I'm at a loss for
something better. Any comments are (again) appreciated.
 
A

Andreas Kahari

I have a situation something like this:

int b, i, j;

for( i=0 ; i < 7 ; i++ ) {
b=0;
for( j=0 ; j < 8 ; j++ ) {
if( some_func() || other_func(i,j) ) {
b=1;
break;
}
}
if( !b ) {
continue;
}
/* do various other things */
}

I'm quite certain this is a bad way to do this, but I'm at a loss for
something better. Any comments are (again) appreciated.

What about this:

int i, j;

for (i = 0; i < 7; ++i) {
for (j = 0; j < 8; ++j) {
if (/* some condition */) {
/* do stuff */
break;
}
}
}


This makes the code easier to read (no magic 'b'). If the code
beyond the double loop needs 'b', then include "b=1" in /* do
stuff */.
 
C

Christopher Benson-Manica

Andreas Kahari said:
int i, j;
for (i = 0; i < 7; ++i) {
for (j = 0; j < 8; ++j) {
if (/* some condition */) {
/* do stuff */
break;
}
}
}

Hm, that certainly looks nice... I may give that a try, although since
do_stuff is quite involved, I'd probably have to move it to a separate
function for the above approach to be readable. Thanks.
 
E

Ed Morton

I have a situation something like this:

int b, i, j;

for( i=0 ; i < 7 ; i++ ) {
b=0;
for( j=0 ; j < 8 ; j++ ) {
if( some_func() || other_func(i,j) ) {
b=1;
break;
}
}
if( !b ) {
continue;
}
/* do various other things */
}

I'm quite certain this is a bad way to do this, but I'm at a loss for
something better. Any comments are (again) appreciated.

Using break, continue, and goto let you jump from one point in your code to
another without explicitly stating the abstraction of why you're doing it. I
don't want to start another debate on whether or not that's reasonable. Who
knows, maybe it is for some people. I personally always try to make the
conditions that cause me to make execution flow changes explicit and I find that
if I never let myself use break (except in case statements), continue, or goto,
then that forces me to either explicitly state those conditions so the next
person to modify the code doesn't have to manually synthesize those
abstractions, or at the very least ensure that if you look at the condition
specified between the two ";"s in any loop, then that is THE condition that
affects the loop entry/exit and there isn't some other nameless exit condition
hidden in the loop body somewhere.

For example, in your loops you behave differently if "some_func() ||
other_func(i,j)" but there's no indication of what the condition MEANS or why
it's important someone coming along to modify that code in future would have to
spend the time to figure out that meaning (synthesize the abstraction).

With that in mind, if we assume that when either "some_func()" or
"other_func(i,j)" return a non-zero value, that means you're out of resources,
then you can get rid of the break and continue and, I think, make the code
easier to read to the next guy:

int i, j;

for( i=0 ; i < 7 ; i++ ) {
int gotRsrcs = 1;
for( j=0 ; j < 8 && gotRsrcs; j++ ) {
gotRsrcs = ( (some_func() || other_func(i,j)) ? 0 : 1 )
}
if( gotRsrcs ) {
/* do various other things */
}
}

Regards,

Ed.
 
R

Robert Stankowic

Christopher Benson-Manica said:
I have a situation something like this:

int b, i, j;

for( i=0 ; i < 7 ; i++ ) {
b=0;
for( j=0 ; j < 8 ; j++ ) {
if( some_func() || other_func(i,j) ) {

Are you aware, that other_func() is only called, if some_func() returns 0 ?
If other_func() has side effects this can be a problem. For example, if you
assign the return value of other_func() to a variable inside the if() you
may end up with an uninitialized variable.
b=1;
break;
}
}
if( !b ) {
continue;
}
/* do various other things */
}

What about

for( i=0 ; i < 7 ; i++ )
{
for(j = 0; j < 8 && !b; j++)
{
if( some_func() || other_func(i,j) )
{
b = 1;
}
else
{
/* do various things */
}
}
if(b)
{
/* do various other things */
}
}

Just my $0.02
Robert
 
C

Christopher Benson-Manica

Robert Stankowic said:
Are you aware, that other_func() is only called, if some_func() returns 0 ?
If other_func() has side effects this can be a problem. For example, if you
assign the return value of other_func() to a variable inside the if() you
may end up with an uninitialized variable.

Thanks for pointing that out. Actually, other_func() isn't really a function
(more like a 2-D array indexed by i and j, but I thought the function was
clearer for the purposes of my pseudocode), so it isn't an issue. I can't
guarantee, however, that I would have avoided it if it *had* been an issue,
though, so again, thanks :)
 
R

Robert Stankowic

Christopher Benson-Manica said:
Thanks for pointing that out. Actually, other_func() isn't really a function
(more like a 2-D array indexed by i and j, but I thought the function was
clearer for the purposes of my pseudocode), so it isn't an issue. I can't
guarantee, however, that I would have avoided it if it *had* been an issue,
though, so again, thanks :)

Hi,
funny enough I can see your response to my post, but not my post itself :(
Anybody else the same problem?
Thanks
Robert
 
E

E. Robert Tisdale

Christopher said:
I have a situation something like this:

int b, i, j;

for( i=0 ; i < 7 ; i++ ) {
b=0;
for( j=0 ; j < 8 ; j++ ) {
if( some_func() || other_func(i,j) ) {
b=1;
break;
}
}
if( !b ) {
continue;
}
/* do various other things */
}

I'm quite certain this is a bad way to do this
but I'm at a loss for something better.
Any comments are (again) appreciated.

const int m = 7;
const int n = 8;
bool b = false;
for(int i = 0; !b && i < m; ++i) {
for(int j = 0; !b && j < n; ++j) {
b = some_func() || other_func(i, j);
}
if (b) {
// do various other things
}
}
 

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
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top