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

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

  1. utab

    utab Guest

    Why is for(int i=0; i < 100; ++i) poor?
     
    utab, Aug 29, 2006
    #1
    1. Advertising

  2. utab

    Noah Roberts Guest

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


    poor for what?
     
    Noah Roberts, Aug 29, 2006
    #2
    1. Advertising

  3. utab

    utab Guest


    > 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
    #3
  4. 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.

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 29, 2006
    #4
  5. utab

    utab Guest

    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.
     
    utab, Aug 29, 2006
    #5
  6. utab

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


    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
    #6
  7. 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
    #7
  8. utab

    utab Guest

    Sorry but maybe I should have asked

    why is

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

    is poor?
     
    utab, Aug 29, 2006
    #8
  9. 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
    #9
  10. utab

    Kaz Kylheku Guest

    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
    #10
  11. utab

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


    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
    #11
  12. utab

    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
    #12
  13. 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
    #13
  14. utab

    Jim Langston Guest

    "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
    #14

  15. > 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
    #15
  16. utab

    Salt_Peter Guest

    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
    #16
  17. utab

    utab Guest


    > 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
    #17
  18. utab

    Earl Purple Guest

    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
    #18
  19. utab

    James Guest

    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
    #19
  20. utab

    Ian Collins Guest

    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
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,236
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,682
    Old Wolf
    Jan 20, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  4. fred
    Replies:
    3
    Views:
    291
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    904
Loading...

Share This Page