Why is for(int i=0; i < 100; ++i) poor?

O

Old Wolf

Andrew said:
As noted, the exercise appears at the end of section 2.4. If you read
section 2.4, you will find that the exercise is answered in the first few
paragraphs of that section.

I am curious and don't have that text. Would you be so kind
as to give the answer, for the benefit of me and others who
also don't have the text?
 
K

kim

for (int i = 100; --i >= 0;)
{
// process i
}

is superior than the other solution : comparing an integer to 0 is more
effective than comparing to any other value.

Are ints unsigned by default on most of your platforms?
Otherwise, on what microprocessor architecture is it faster to check if
a _signed_ int is larger than or equal to 0? :)
 
F

Frederick Gotham

posted:
Are ints unsigned by default on most of your platforms?
Otherwise, on what microprocessor architecture is it faster to check if
a _signed_ int is larger than or equal to 0? :)


int is signed on all platforms.
 
R

Rui Maciel

Nils O. Selåsdal said:
Then it's a trick question, or it's a question asked by a silly
person.

Indeed it was one of the worse formulated and fundamented questions that I
ever saw in a usenet post.


Best regards
Rui Maciel
 
M

Murali Krishna

utab said:
Sorry but maybe I should have asked

why is

for(int i=0; i < 100; ++i)
//process i

is poor?

if you process only i for no reason, the loop is useless or the loop is
meant for delaying the process. If the loop has flower brackets and
processes i in that, I think it is a bad programming practice unless it
has a reason or condition to do so. Otherwise, i can be be processed in
the incrementor of the for loop.

-- Murali Krishna.
 
E

Earl Purple

utab said:
Why is for(int i=0; i < 100; ++i) poor?

Actually I have a for_n algortihm so I can call:

for_n( 100, functor );

or is it the other way round I can't remember.
The functor can maintain a count if it needs it.
 
S

Suma Shanbhog

Newbie here....
Don't you think that the control variable("i" in this case) is pre
incremented which is different from the usual way......??
I have mostly seen examples where the control variable is post
incremented.........
Does it makes a difference?
 
M

Marcus Kwok

Suma,
Please do not top-post. Your responses belong either below, or
interspersed with, the trimmed quoted text that you are replying to.

Suma Shanbhog said:
Newbie here....
Don't you think that the control variable("i" in this case) is pre
incremented which is different from the usual way......??
I have mostly seen examples where the control variable is post
incremented.........
Does it makes a difference?

This is addressed in the FAQ:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15

Also, since you say you're a newbie, you should read the entire FAQ.
There's lots of good stuff in there.
 
S

Suma Shanbhog

Marcus Kwok said:
Suma,
Please do not top-post. Your responses belong either below, or
interspersed with, the trimmed quoted text that you are replying to.


Sorry!
Was not aware!

Suma
 

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
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top