A style question

F

Frederick Ding

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);
}
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!
 
C

Chuck F.

Frederick said:
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);
}
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)) {
notdone = 0; break;
}
notdone &= (a != 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.
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Frederick said:
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);
}
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);
return 1;
}

int empty_array(int arr[], size_t nelem)
{
size_t i;
for(i = 0; i < nelem; i++)
if(arr != 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
 
B

Boudewijn Dijkstra

Frederick said:
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);
if (a == 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.
 
C

Christopher Benson-Manica

Chuck F. said:
do {
for (i = 0, notdone = 1; i < COUNT; i++) {
if (1 != scanf("%d", &a)) {
notdone = 0; break;
}
notdone &= (a != 0);
}
if (notdone) do_other_things();

} /* You're still rusty :) */
 

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
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top