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

Discussion in 'C++' started by utab, Aug 29, 2006.

1. ### utabGuest

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

utab, Aug 29, 2006

2. ### Noah RobertsGuest

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

poor for what?

Noah Roberts, Aug 29, 2006

3. ### utabGuest

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

utab, Aug 29, 2006
4. ### Frederick GothamGuest

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

Reformulate your question and be specific.

--

Frederick Gotham

Frederick Gotham, Aug 29, 2006
5. ### utabGuest

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,

utab, Aug 29, 2006
6. ### Noah RobertsGuest

utab wrote:
> > 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.

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.

Noah Roberts, Aug 29, 2006
7. ### Thomas J. GritzanGuest

utab schrieb:
> 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...

--
Thomas

Thomas J. Gritzan, Aug 29, 2006
8. ### utabGuest

Sorry but maybe I should have asked

why is

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

is poor?

utab, Aug 29, 2006
9. ### =?ISO-8859-15?Q?Juli=E1n?= AlboGuest

utab wrote:

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

Because the units are cents, the you only have an euro at the end.

--
Salu2

=?ISO-8859-15?Q?Juli=E1n?= Albo, Aug 29, 2006
10. ### Kaz KylhekuGuest

utab wrote:
> 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.

Kaz Kylheku, Aug 29, 2006
11. ### Schizoid ManGuest

utab wrote:
>> 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.

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.

Schizoid Man, Aug 29, 2006
12. ### Daniel T.Guest

In article <>,
"utab" <> wrote:

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

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

Daniel T., Aug 29, 2006
13. ### =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=Guest

utab wrote:
>> 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.

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.

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Aug 29, 2006
14. ### Jim LangstonGuest

"utab" <> wrote in message
news:...
> 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?

Jim Langston, Aug 29, 2006
15. ### Gernot FrischGuest

> 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();

Gernot Frisch, Aug 29, 2006
16. ### Salt_PeterGuest

utab wrote:
> 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.

Salt_Peter, Aug 29, 2006
17. ### utabGuest

> 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

utab, Aug 29, 2006
18. ### Earl PurpleGuest

utab wrote:
> 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)

Earl Purple, Aug 29, 2006
19. ### JamesGuest

utab wrote:
> 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

James, Aug 29, 2006
20. ### Ian CollinsGuest

James wrote:
> utab wrote:
>
>>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.
>

Required if you want it to compile!

--
Ian Collins.

Ian Collins, Aug 29, 2006