By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,510 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

A style question

P: n/a
Hi, guys!
I have two questions of some kind of style problem:
1. Is there a better one that can substitute this:
while (1) {
for (i = 0; i < 6; i++) {
scanf("%d", &a[i]);
}
if ((a[0] == 0) && (a[1] == 0) && (a[2] == 0) && (a[3] == 0) &&
(a[4] == 0) && (a[5] == 0))
break;
else {
/* do other things */

}
}
I doubt that the while(1) is decent or not?
Can I find a better one than the "forever" loop?
2. still in the code above:
if (i == 0) break;
else {
}

or

if (i == 0) break;
...... /* do other things without else */

which one is better?
Thanks!
Dec 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Frederick Ding wrote:
Hi, guys!
I have two questions of some kind of style problem:
1. Is there a better one that can substitute this:
Yes, see below.
while (1) {
for (i = 0; i < 6; i++) {
scanf("%d", &a[i]);
}
if ((a[0] == 0) && (a[1] == 0) && (a[2] == 0) && (a[3] == 0) &&
(a[4] == 0) && (a[5] == 0))
break;
else {
/* do other things */

}
}
I doubt that the while(1) is decent or not?
Can I find a better one than the "forever" loop?
2. still in the code above:
if (i == 0) break;
else {
}

or

if (i == 0) break;
...... /* do other things without else */

which one is better?


Here's another way to write the code above. First of all we create a
couple of functions, one that reads numbers and one that tests the array
for emptiness. As you see, these are general functions operating on
arrays of any size. The functions can be optimized later and you can
even add error handling and asserts...

int read_numbers(FILE* f, int dest[], size_t nelem)
{
size_t i;
for(i = 0; i < nelem; i++)
fscanf(f, "%d", &dest[i]);
return 1;
}

int empty_array(int arr[], size_t nelem)
{
size_t i;
for(i = 0; i < nelem; i++)
if(arr[i] != 0)
return 0;

return 1;
}

Back to your loop. No more breaks, only one magic number and no
while(1). So much nicer, isn't it?
....
int myarray[6];
size_t nelem = sizeof myarray / sizeof *myarray;

while(read_numbers(stdin, myarray, nelem)
&& !array_empty(myarray, nelem)) {
/* Do stuff */
}

....
Boa
Dec 15 '05 #2

P: n/a
Frederick Ding wrote:

I have two questions of some kind of style problem:
1. Is there a better one that can substitute this:
while (1) {
for (i = 0; i < 6; i++) {
scanf("%d", &a[i]);
}
if ((a[0] == 0) && (a[1] == 0) && (a[2] == 0) &&
(a[3] == 0) && (a[4] == 0) && (a[5] == 0))
break;
else {
/* do other things */

}
}
I doubt that the while(1) is decent or not?
Can I find a better one than the "forever" loop?


How about:

do {
for (i = 0, notdone = 1; i < COUNT; i++) {
if (1 != scanf("%d", &a[i])) {
notdone = 0; break;
}
notdone &= (a[i] != 0);
}
if (notdone) do_other_things();
while (notdone);

Note the testing of scanf return, although the action on failure
may not be what you want. scanf results should always be checked.

The main point is that doing something and testing the result is
better characterized by a do while loop. Also altering COUNT
should not require nitty-gritty changes in the exit test.

--
Read about the Sony stealthware that is a security leak, phones
home, and is generally illegal in most parts of the world. Also
the apparent connivance of the various security software firms.
http://www.schneier.com/blog/archive...drm_rootk.html
Dec 15 '05 #3

P: n/a
Frederick Ding wrote:
Hi, guys!
I have two questions of some kind of style problem:
1. Is there a better one that can substitute this:
Yes. If you don't like functions, I would suggest the following
replacement to Bjørns beautiful piece of work:

int a[6];
int nZeros;

while (1) {
nZeros = 0;
for (i = 0; i < 6; i++) {
scanf("%d", &a[i]);
if (a[i] == 0) {
nZeros++;
}
}
if (nZeros == 6) {
break;
}

/* do other things */
}
I doubt that the while(1) is decent or not?


As long as the loop termination condition is obvious or as long as it is
obviously meant to be an infinite loop. The meaning of obvious depends
on the readers of your code.
Dec 15 '05 #4

P: n/a
Chuck F. <cb********@yahoo.com> wrote:
do {
for (i = 0, notdone = 1; i < COUNT; i++) {
if (1 != scanf("%d", &a[i])) {
notdone = 0; break;
}
notdone &= (a[i] != 0);
}
if (notdone) do_other_things(); } /* You're still rusty :-) */ while (notdone);


--
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.


Dec 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.