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

F

Frederick Gotham

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


utab, you post here regularly. One would hope that, over time, the quality of
your posts would improve.

Don't ask questions which we can't answer, unless you want us to give a
smart-alec answer.

Reformulate your question and be specific.
 
U

utab

Frederick, you are following my posts, interesting to have some fans
here ;-)
utab, you post here regularly. One would hope that, over time, the quality of
your posts would improve.

What is wrong with my question, I have supplied the necessary info.
where I had come up with that if you have an idea and want to share,
please type.
 
N

Noah Roberts

utab said:
I do not know, while looking up for sth inside C++ primer I came up
with an exercise question? That's it.

If there is no context then the question is bull shit and you should
ignore it. For loops are totally acceptable for certain tasks and in
fact are often the best way to accomplish things. Generally saying a
for loop is "poor" is bunk.

Now, a better question might be why might you prefer for_each over an
equivelant for loop. There are actually answers to this question.
Using algorithms in the std over your own loops has the possibility of
being faster due to the use of private parts in the standard library.
It is also cleaner for many tasks (though not all) and is something
more C++ programmers should be used to seing. Finally it keeps the
check code from being executed more than once without explicitly
declaring a variable or depending on compiler optimizations to get rid
of it.

Does this mean a for loop is "poor"? Hell no. Maybe that book is
garbage - I don't know as I've never read it.
 
T

Thomas J. Gritzan

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

To iterate is human, to recurse divine.
-L. Peter Deutsch

There may be many reasons:
* Use of magic number '100'
* Doesn't compile: Missing statement
* Useless, because there are no side effects
* Could be done with a STL algorithm

Depends...
 
U

utab

Sorry but maybe I should have asked

why is

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

is poor?
 
K

Kaz Kylheku

utab said:
Sorry but maybe I should have asked

why is

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

is poor?

Because it's a syntax error. The for (;;) syntax must be followed by
a statement.
 
S

Schizoid Man

utab said:
I do not know, while looking up for sth inside C++ primer I came up
with an exercise question? That's it.

What exactly is 'sth inside C++ primer'?

Most C++ texts are appalling, and my guess would be that this one is no
different.

The only remotely reasonable answer I can come up with is that the loop
test condition should be more rigid: <= instead of <.

However without the given context, the answer above may be completely wrong.
 
D

Daniel T.

poor for what?

I do not know, while looking up for sth inside C++ primer I came up
with an exercise question? That's it.[/QUOTE]

utab, the book C++ Primer is over 1,200 pages. Go ahead and provide the
context so we can explain what Mr. Lippman meant.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

utab said:
I do not know, while looking up for sth inside C++ primer I came up
with an exercise question? That's it.
The question had those exact words, and no other context ?

Then it's a trick question, or it's a question asked by a silly
person.
 
J

Jim Langston

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

In and of itself it is not poor. The only mentionable thing is 100 is a
magic number. What does it mean? Other than that, it's fine if that's what
you want to do.

If, however, you are iterating thorugh an STL container with 100 elements,
using an iterator would be better.

Taken out of context, there is nothing wrong with that. Taken in the
context of what the author was actually talking about, it may be poor. You
haven't given enough information.

Why is blue better than green?
 
G

Gernot Frisch

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

It's too small. You really should consider lloping over at least 512
cycles.
like:
for(int i=0; i<512; ++i)
rich();
 
S

Salt_Peter

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

Consider a few examples that rely on the size of the container you are
acting upon. That fixed value of 100 implies a primitive, fixed-size
container. Iterators are a better solution, what if i decided to use a
std::list or std::map instead of a std::vector? Add to that that
algorithms (copy, for_each or transform) may offer yet a better
solution ( these are often optimized for the specific container).

#include <iostream>
#include <vector>

void display( std::vector< int >& r_v )
{
typedef std::vector< int >::iterator VIter;
VIter iter = r_v.begin();
for (iter; iter != r_v.end(); ++iter)
{
std::cout << *iter << std::endl;
}
}

int main()
{
std::vector< int > vn(5, 0); // 5 elements, all 0

for( size_t n = 0; n < vn.size(); ++n )
{
std::cout << vn[ n ] << std::endl;
}

display( vn ); // with iterators

return 0;
}

I could have also passed the number of elements required through the
program's parameter list.
 
U

utab

utab, the book C++ Primer is over 1,200 pages. Go ahead and provide the
context so we can explain what Mr. Lippman meant.

I do not know what is meant by Mr.Lippman. Since I do not have a paper
copy, I can not give an exact page number but the exact exercise number
is

Exercises Section 2.4

Exercise 2.22: The following program fragment, while legal, is an
example of poor style. What problem(s) does it contain? How would you
improve it?

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

Earl Purple

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

Maybe because 'i' is not descriptive and 100 is a magic number?
(although I thought 3 was the magic number)
 
J

James

utab said:
Sorry but maybe I should have asked

why is

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

is poor?

I think that

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

is prefered.

James
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top