Thread: while (a++ < 10)

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794

    while (a++ < 10)

    Is this a 'bug' I think I have had probs with this and have had to
    change it to:-

    Code:
    a=0;
    While(a<10) {
    ....
    ....
    a++
    }
    The example in the title seemed to skip the case where a=0
    and start off with a=1.

    I was under the impression that a++ meant the increment
    was done before the test as in the asignment:-
    Code:
    a=0
    b=a++; /* b now 0 */
    but:-
    Code:
    a=0;
    b=++a; /* b now 1 */

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    There are two forms of the increment operator. The prefix and the postfix version. The prefix version is ++a and the postfix version a++. Both have the same result if used by themselves but in when used in statements like the above there is a difference. In your assignment example, b=a++, b is assigned the current value of a and then a is incremented. In b=++a, a is incremented first then the result is assigned to b.

    I was under the impression that a++ meant the increment
    was done before the test as in the asignment:-
    Last edited by silk.odyssey; 05-19-2006 at 07:28 PM.
    silk.odyssey

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by silk.odyssey
    There are two forms of the increment operator. The prefix and the postfix version. The prefix version is ++a and the postfix version a++. Both have the same result if used by themselves but in when used in statements like the above there is a difference. In your assignment example, b=a++, b is assigned the current value of a and then a is incremented. In b=++a, a is incremented first then the result is assigned to b.
    Yes I know that, but what happens when it is not an assignment
    but a test condition situation?? It seems the increment is done
    before the test irregardless of ++a or a++.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Since you haven't shown the actual body of the while look its hard to know what behavior you are seeing. However lets examine two cases:

    Code:
    int a = 0;
    while ( a++ < 10 )
      printf("%d\n", a);
    In this case it will print the numbers from 1 to 10 inclusivly.

    The other case would be:
    Code:
    int a = 0;
    while ( ++a < 10 )
      printf("%d\n", a);
    Which would print the numbers from 1 to 9.

    From your example it appears you are using the value of a inside your while loop. If this is true then you don't want to do the increment inside the while condition which is executed before the body of the loop.

    A for loop appears to be what you would want to use.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Thantos
    Since you haven't shown the actual body of the while look its hard to know what behavior you are seeing. However lets examine two cases:

    Code:
    int a = 0;
    while ( a++ < 10 )
      printf("%d\n", a);
    In this case it will print the numbers from 1 to 10 inclusivly.

    The other case would be:
    Code:
    int a = 0;
    while ( ++a < 10 )
      printf("%d\n", a);
    Which would print the numbers from 1 to 9.

    From your example it appears you are using the value of a inside your while loop. If this is true then you don't want to do the increment inside the while condition which is executed before the body of the loop.

    A for loop appears to be what you would want to use.
    Both your example are the same.
    However I know the increment is done before the test cos it
    fixed a bug.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Futher more I think it dates back to machine code where u can have
    a=a++ (register a).
    There would not be a machhine code to do the increment after the test
    which is why I have previously ran into probs.
    A weakness in the language I would say.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The behavior of this is undefined.
    Quote Originally Posted by esbo
    a=a++ (register a).
    http://c-faq.com/expr/ieqiplusplus.html


    >Both your example are the same.
    No, they are as described.

    Think about this:
    Code:
       while ( a++ < 10 )
          printf("%d\n", a);
    Compare the current value of a with 10, then increment a and enter the loop.

    And this:
    Code:
       while ( ++a < 10 )
          printf("%d\n", a);
    Increment the value of a, then compare it with 10 and enter the loop.

    In either case, a is incremented before it enters the loop. If a is 0 before the loop, then in either case it will be 1 on the first iteration.

    Now,
    Code:
    a=0;
    while(a<10) {
    a++
    }
    Do nothing to a, compare to 10, the enter the loop (which increments a).

    All three are different. All three produce different results.
    Last edited by Dave_Sinkula; 05-19-2006 at 08:37 PM. Reason: D'oh! Copy and paste bug. Not. (Second case was not preincrement, it was copy of postincrement I forgot to change.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A weakness in the language I would say.
    Why is that? Increment and decrement operations have precedence over comparison operators because they might be part of the test condition. If you tested before doing math it would mess up a lot of logic.

    Can you add true+1?

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Dave_Sinkula
    The behavior of this is undefined.http://c-faq.com/expr/ieqiplusplus.html


    >Both your example are the same.
    No, they are as described.

    Think about this:
    Code:
       while ( a++ < 10 )
          printf("%d\n", a);
    Compare the current value of a with 10, then increment a and enter the loop.

    And this:
    Code:
       while ( ++a < 10 )
          printf("%d\n", a);
    Increment the value of a, then compare it with 10 and enter the loop.

    In either case, a is incremented before it enters the loop. If a is 0 before the loop, then in either case it will be 1 on the first iteration.

    Now,
    Code:
    a=0;
    while(a<10) {
    a++
    }
    Do nothing to a, compare to 10, the enter the loop (which increments a).

    All three are different. All three produce different results.
    I think the problem with is while ( ++a < 10 ) is that you would
    have to set a to one less than the first value you wanted to
    process make it work, so in a lot of my code I had to set variables
    to -1 to produce the desired result, 0 which is a bit unnatural.

    My 'flawed' thinking made me expect the loop would be processed
    once before "a" would be incremented.
    In future I will make all such loops:-

    Code:
    a=0;
    while(a<10) {
    ...
    a++
    }
    This removes any confusion in my mind as to what is happening
    and it should save me a lot of grief debugging code. I would be
    nice to have a loop to do what I wanted it to do though.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by citizen
    Why is that? Increment and decrement operations have precedence over comparison operators because they might be part of the test condition. If you tested before doing math it would mess up a lot of logic.

    Can you add true+1?
    I think that they are more suited to a computers mind than a
    humans, for example I would have to remember the above
    statement when reading and writing code!!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > nice to have a loop to do what I wanted it to do though.
    You've taken a very long road just to invent
    for ( a = 0 ; a < 10 ; a++ )

    No wait, there's nothing to invent, C does this anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Salem
    > nice to have a loop to do what I wanted it to do though.
    You've taken a very long road just to invent
    for ( a = 0 ; a < 10 ; a++ )

    No wait, there's nothing to invent, C does this anyway.
    I guess so, I guess I get a little uneasy with the language cos
    I would expect:-

    Code:
    for ( a = 0 ; a < 10 ; a++ )
    to be similar to:-

    Code:
    a=0;
    while ( a ++< 10 )
    Because I would expect everything within the brackets to be done
    at the same time for consistancy.
    Basically I am just to lazy to read the manual
    Maybe it is because I am used to evaluating mathematical expressions and the for loop 'breaks the rules'.

    It is a bit of a case things put in to make things easier for
    me have made things harder for me.

    Also you can't put debug statements into the for
    statement? Then again maybe I would need em if I used
    it.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also you can't put debug statements into the for statement?
    You can put debugging statements anywhere you can put normal statements. Inside loops, functions, switch statments, if statements, anywhere you want.

    [edit]
    This
    Code:
    for(a = 0; a < 10; a ++) {
        function();
    }
    is the same as
    Code:
    a = 0;
    while(a < 10) {
        function();
        a ++;
    }
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks
    You can put debugging statements anywhere you can put normal statements. Inside loops, functions, switch statments, if statements, anywhere you want.

    [edit]
    This
    Code:
    for(a = 0; a < 10; a ++) {
        function();
    }
    is the same as
    Code:
    a = 0;
    while(a < 10) {
        function();
        a ++;
    }
    [/edit]
    How about:-

    Code:
    for(a=0; function(); a<10; function; a++; function()){
    
    }

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Maybe it is because I am used to evaluating mathematical expressions and the for loop 'breaks the rules'.
    I find this funny because Dennis Ritchie is a mathematian and Brian Kernighan is an engineer. So both come from a heavy math based background. Almost all of C's expressions come from a math base.

    It might be my mind going but IIRC the for loop wasn't present in the early stages of C. It was added later as a nicer way to package loops up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file
    By nhubred in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2009, 11:34 AM
  2. A-Star Pathfinding
    By mike_g in forum General AI Programming
    Replies: 1
    Last Post: 08-05-2007, 04:18 PM
  3. Segmentation Fault
    By Lost__Soul in forum C Programming
    Replies: 46
    Last Post: 04-28-2003, 04:24 AM
  4. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM
  5. Heaps...
    By Nutshell in forum C Programming
    Replies: 14
    Last Post: 04-23-2002, 08:54 AM