473,379 Members | 1,355 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,379 software developers and data experts.

A style question

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
4 1085
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
33
by: amerar | last post by:
Hi All, I can make a page using a style sheet, no problem there. However, if I make an email and send it out to my list, Yahoo & Hotmail totally ignore the style tags. It looks fine in...
1
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I...
4
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
83
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
39
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A...
18
by: pocmatos | last post by:
Hi all, While I was programming 5 minutes ago a recurring issue came up and this time I'd like to hear some opinions on style. Although they are usually personal I do think that in this case as...
3
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It...
6
by: MatthewS | last post by:
I've seen the question raised several times here, but apparently never answered. Since PyInstance_Check returns False for new-style class instances, is there a standard procedure for testing this...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.