473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

More loop style stuff

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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #1
7 1306
In article <bo**********@chessie.cirr.com>, Christopher Benson-Manica wrote:
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 */.
--
Andreas Kähäri
Nov 13 '05 #2
Andreas Kahari <ak*******@freeshell.org> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #3


On 11/11/2003 8:10 AM, Christopher Benson-Manica wrote:
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.

Nov 13 '05 #4

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> schrieb im
Newsbeitrag news:bo**********@chessie.cirr.com...
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
Nov 13 '05 #5
Robert Stankowic <pc******@netway.at> spoke thus:
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 :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #6

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> schrieb im
Newsbeitrag news:bo**********@chessie.cirr.com...
Robert Stankowic <pc******@netway.at> spoke thus:
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 :)


Hi,
funny enough I can see your response to my post, but not my post itself :(
Anybody else the same problem?
Thanks
Robert
Nov 13 '05 #7
Christopher Benson-Manica wrote:
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
}
}

Nov 13 '05 #8

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

Similar topics

22
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway...
16
by: TheKeith | last post by:
I'm writing a script with a while loop in it. How can I get it to go slower so that it doesn't appear to happen all at once--so that it looks animated--basically. I tried the setTimeout(500) in the...
6
by: sudeep | last post by:
Implementation of BigInt Problem Statement The int basic data type provided by the C/C++ language to represent integers has the following limitations, viz., the size of the data type is machine...
1
by: Dave R | last post by:
Hey everybody! I'm in dire need of some help. I'm trying to loop through all the elements in a form, and determine whether or not the field is visible. Kinda like this: var TForm =...
4
by: Christopher Benson-Manica | last post by:
I have a situation something like this: int foo; for( foo=bar() ; foo <= bar()+1 ; foo++ ) { if( !baz(foo) ) break; /* do stuff with foo */ } The idea is that the loop happens at most...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
8
by: Jim Langston | last post by:
There's the thing about iterating though a map or vector when you may delete one of the elements, where you simply assign the iterator to the map.erase() statement or increment it if you don't. ...
14
by: dawnerd | last post by:
Hi, I am developing a CMS and came across something which has never happened to me before, and I re-wrote the specific script twice, both differently, and still had the same error. I'm not sure...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...

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.