Diffrence between ++i and i++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luai

    #1

    Diffrence between ++i and i++

    I made my midterm exam in the C language course.
    I lost 18 marks off 100 because I didn't relaize this killing fact:

    in (for loops) there is no difference between incrementing the loop in
    these two ways:

    for (i=0; i < 10 ; ++i)

    and

    for (i=0; i < 10 ; i++)

    ( notice the difference is between i++ and ++i )

    What are your comments on this.
  • Ben Pfaff

    #2
    Re: Diffrence between ++i and i++

    lrantisi@yahoo. com (Luai) writes:
    [color=blue]
    > in (for loops) there is no difference between incrementing the loop in
    > these two ways:
    >
    > for (i=0; i < 10 ; ++i)
    >
    > and
    >
    > for (i=0; i < 10 ; i++)[/color]

    Why and how did you think they might be different?

    Comment

    • Mac

      #3
      Re: Diffrence between ++i and i++

      On Sun, 11 Apr 2004 20:26:04 +0000, Luai wrote:
      [color=blue]
      > I made my midterm exam in the C language course.
      > I lost 18 marks off 100 because I didn't relaize this killing fact:
      >
      > in (for loops) there is no difference between incrementing the loop in
      > these two ways:
      >
      > for (i=0; i < 10 ; ++i)
      >
      > and
      >
      > for (i=0; i < 10 ; i++)
      >
      > ( notice the difference is between i++ and ++i )
      >
      > What are your comments on this.[/color]

      Sounds about right. Usually the meaning of ++i and i++ is one of the first
      things you learn in c. So, if you got all the way to the midterm without
      learning this, it is not a good sign. Now you know you need to study a
      little more before the final if you want a good grade (mark).

      HTH

      --Mac

      Comment

      • Irrwahn Grausewitz

        #4
        Re: Diffrence between ++i and i++

        lrantisi@yahoo. com (Luai) wrote:[color=blue]
        >I made my midterm exam in the C language course.
        >I lost 18 marks off 100 because I didn't relaize this killing fact:
        >
        >in (for loops) there is no difference between incrementing the loop in
        >these two ways:
        >
        >for (i=0; i < 10 ; ++i)[/color]

        which is equivalent to:

        i = 0;
        while ( i < 10 )
        {
        ++i;
        }
        [color=blue]
        >and
        >
        >for (i=0; i < 10 ; i++)[/color]

        which is equivalent to:

        i = 0;
        while ( i < 10 )
        {
        i++;
        }
        [color=blue]
        >What are your comments on this.[/color]

        Since in both cases the iteration statement is evaluated only
        for its side effect (increment i), while its value is discarded,
        there's effectively no difference.

        HTH
        Regards
        --
        Irrwahn Grausewitz (irrwahn33@free net.de)
        welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
        clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
        clc OT guide : http://benpfaff.org/writings/clc/off-topic.html

        Comment

        • Edward E. Hopkins

          #5
          Re: Diffrence between ++i and i++

          Predecrement vs. postdecrement ... the ++ in front of something increments
          and then evaluates ... the ++ after something evaluates and then increments.

          Ed


          "Luai" <lrantisi@yahoo .com> wrote in message
          news:64bff751.0 404111926.1c617 44e@posting.goo gle.com...[color=blue]
          > I made my midterm exam in the C language course.
          > I lost 18 marks off 100 because I didn't relaize this killing fact:
          >
          > in (for loops) there is no difference between incrementing the loop in
          > these two ways:
          >
          > for (i=0; i < 10 ; ++i)
          >
          > and
          >
          > for (i=0; i < 10 ; i++)
          >
          > ( notice the difference is between i++ and ++i )
          >
          > What are your comments on this.[/color]


          Comment

          • Malcolm

            #6
            Re: Diffrence between ++i and i++


            "Luai" <lrantisi@yahoo .com> wrote in message[color=blue]
            >
            > I made my midterm exam in the C language course.
            > I lost 18 marks off 100 because I didn't relaize this killing fact:
            >[/color]
            A penalty of 18% for not realising that ++i and i++, in some situations,
            have exactly the same effect sounds rather harsh. However at least you know
            now.


            Comment

            • Andrew Clark

              #7
              Re: Diffrence between ++i and i++

              *** post for FREE via your newsreader at post.newsfeed.c om ***

              Ben Pfaff <blp@cs.stanfor d.edu> wrote in
              news:87oepx50pr .fsf@blp.benpfa ff.org:
              [color=blue]
              > lrantisi@yahoo. com (Luai) writes:
              >[color=green]
              >> in (for loops) there is no difference between incrementing the loop in
              >> these two ways:
              >>
              >> for (i=0; i < 10 ; ++i)
              >>
              >> and
              >>
              >> for (i=0; i < 10 ; i++)[/color]
              >
              > Why and how did you think they might be different?[/color]

              I recall an exam where I was marked off for writing one of these (I
              forget which one), and to correct it the instructor wrote the other one.

              Andrew


              -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
              http://www.newsfeed.com - The #1 Newsgroup Service in the World!
              -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

              Comment

              • Ben Pfaff

                #8
                Re: Diffrence between ++i and i++

                Andrew Clark <anclark@syr.ed u> writes:
                [color=blue]
                > Ben Pfaff <blp@cs.stanfor d.edu> wrote in
                > news:87oepx50pr .fsf@blp.benpfa ff.org:
                >[color=green]
                >> lrantisi@yahoo. com (Luai) writes:
                >>[color=darkred]
                >>> in (for loops) there is no difference between incrementing the loop in
                >>> these two ways:
                >>>
                >>> for (i=0; i < 10 ; ++i)
                >>>
                >>> and
                >>>
                >>> for (i=0; i < 10 ; i++)[/color]
                >>
                >> Why and how did you think they might be different?[/color]
                >
                > I recall an exam where I was marked off for writing one of these (I
                > forget which one), and to correct it the instructor wrote the other one.[/color]

                You'll have to be more specific. When ++i or i++ is a full
                expression, they are equivalent. When one of them is a
                subexpression of a larger expression, they may not be
                equivalent. So if your instructor took off points in the former
                case, he (or she) was simply wrong, but in the latter case he may
                have been justified.

                By the way, here is the definition of a "full expression", from
                C99 6.8:

                4 A full expression is an expression that is not part of another
                expression or of a declarator. Each of the following is a
                full expression: an initializer; the expression in an
                expression statement; the controlling expression of a
                selection statement (if or switch); the controlling
                expression of a while or do statement; each of the
                (optional) expressions of a for statement; the (optional)
                expression in a return statement. The end of a full
                expression is a sequence point.

                --
                Go not to Usenet for counsel, for they will say both no and yes.

                Comment

                • E. Robert Tisdale

                  #9
                  Re: Diffrence between ++i and i++

                  Luai wrote:
                  [color=blue]
                  > I made my midterm exam in the C language course.
                  > I lost 18 marks off 100 because I didn't realize this killing fact:
                  >
                  > in (for loops) there is no difference
                  > between incrementing the loop in these two ways:
                  >
                  > for (i = 0; i < 10; ++i)
                  >
                  > and
                  >
                  > for (i = 0; i < 10; i++)
                  >
                  > (notice the difference is between i++ and ++i)
                  >
                  > What are your comments on this.[/color]

                  There is no difference.
                  C++ programmers prefer ++i only as a "good habit"
                  because both the pre increment and post increment operator++
                  may be overloaded for a class for very large objects
                  where i++ returns a copy of the original object
                  but ++i merely returns a reference
                  after "incrementi ng" the original object.
                  If you are going to write C++ programs as well as C programs,
                  you probably should use ++i wherever you have a choice.

                  Comment

                  • Andrew Clark

                    #10
                    Re: Diffrence between ++i and i++

                    *** post for FREE via your newsreader at post.newsfeed.c om ***

                    Ben Pfaff <blp@cs.stanfor d.edu> wrote in
                    news:87d66cohs0 .fsf@blp.benpfa ff.org:
                    [color=blue]
                    > Andrew Clark <anclark@syr.ed u> writes:
                    >[color=green]
                    >> Ben Pfaff <blp@cs.stanfor d.edu> wrote in
                    >> news:87oepx50pr .fsf@blp.benpfa ff.org:
                    >>[color=darkred]
                    >>> lrantisi@yahoo. com (Luai) writes:
                    >>>
                    >>>> in (for loops) there is no difference between incrementing the loop
                    >>>> in these two ways:
                    >>>>
                    >>>> for (i=0; i < 10 ; ++i)
                    >>>>
                    >>>> and
                    >>>>
                    >>>> for (i=0; i < 10 ; i++)
                    >>>
                    >>> Why and how did you think they might be different?[/color]
                    >>
                    >> I recall an exam where I was marked off for writing one of these (I
                    >> forget which one), and to correct it the instructor wrote the other
                    >> one.[/color]
                    >
                    > You'll have to be more specific. When ++i or i++ is a full
                    > expression, they are equivalent. When one of them is a
                    > subexpression of a larger expression, they may not be
                    > equivalent. So if your instructor took off points in the former
                    > case, he (or she) was simply wrong, but in the latter case he may
                    > have been justified.
                    >
                    > By the way, here is the definition of a "full expression", from
                    > C99 6.8:
                    >
                    > 4 A full expression is an expression that is not part of another
                    > expression or of a declarator. Each of the following is a
                    > full expression: an initializer; the expression in an
                    > expression statement; the controlling expression of a
                    > selection statement (if or switch); the controlling
                    > expression of a while or do statement; each of the
                    > (optional) expressions of a for statement; the (optional)
                    > expression in a return statement. The end of a full
                    > expression is a sequence point.
                    >[/color]

                    It was the former. If I can find my exam booklet I'll post the problem
                    and solution, but IIRC it was more or less:

                    Write an expression for iterating though an array using a for statement

                    I remember the problem was such that it didn't matter which kind (pre-
                    or post-) the increment operator was.

                    Andrew


                    -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
                    http://www.newsfeed.com - The #1 Newsgroup Service in the World!
                    -----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

                    Comment

                    • John Bode

                      #11
                      Re: Diffrence between ++i and i++

                      lrantisi@yahoo. com (Luai) wrote in message news:<64bff751. 0404111926.1c61 744e@posting.go ogle.com>...[color=blue]
                      > I made my midterm exam in the C language course.
                      > I lost 18 marks off 100 because I didn't relaize this killing fact:
                      >
                      > in (for loops) there is no difference between incrementing the loop in
                      > these two ways:
                      >
                      > for (i=0; i < 10 ; ++i)
                      >
                      > and
                      >
                      > for (i=0; i < 10 ; i++)
                      >
                      > ( notice the difference is between i++ and ++i )
                      >
                      > What are your comments on this.[/color]

                      A proper understanding of the pre and post ++ and -- operators is
                      vital if you're going to be programming in C. I don't know if that
                      particular question should have been worth 18 points, though.

                      The expression i++ evaluates to the current value of i; as a *side
                      affect*, i is incremented some time before the next sequence point.

                      The expression ++i evaluates to the current value of i *plus 1*; as a
                      *side affect*, i is incremented sometime before the next sequence
                      point.

                      The -- operator works the same way, just replace "plus" with "minus"
                      and "increment" with "decrement" .

                      In both cases above, you don't care about what the expression
                      evaluates to, just the side effect (incrementing i by 1), so either
                      expression works just as well. OTOH, if the autoincrement/decrement
                      expression is part of a larger expression, one or the other may be
                      called for. For example, given a variable i initialized to 1, the
                      expressions

                      j = i++ * 2 /* i == 1, j = 1 * 2 */

                      and

                      j = ++i * 2 /* i == 1, j = 2 * 2 */

                      will assign different results for j, so whether you use pre- or
                      post-increment does matter.

                      Comment

                      • Christian Bau

                        #12
                        Re: Diffrence between ++i and i++

                        In article <87d66cohs0.fsf @blp.benpfaff.o rg>,
                        Ben Pfaff <blp@cs.stanfor d.edu> wrote:
                        [color=blue]
                        > Andrew Clark <anclark@syr.ed u> writes:
                        >[color=green]
                        > > Ben Pfaff <blp@cs.stanfor d.edu> wrote in
                        > > news:87oepx50pr .fsf@blp.benpfa ff.org:
                        > >[color=darkred]
                        > >> lrantisi@yahoo. com (Luai) writes:
                        > >>
                        > >>> in (for loops) there is no difference between incrementing the loop in
                        > >>> these two ways:
                        > >>>
                        > >>> for (i=0; i < 10 ; ++i)
                        > >>>
                        > >>> and
                        > >>>
                        > >>> for (i=0; i < 10 ; i++)
                        > >>
                        > >> Why and how did you think they might be different?[/color]
                        > >
                        > > I recall an exam where I was marked off for writing one of these (I
                        > > forget which one), and to correct it the instructor wrote the other one.[/color]
                        >
                        > You'll have to be more specific. When ++i or i++ is a full
                        > expression, they are equivalent. When one of them is a
                        > subexpression of a larger expression, they may not be
                        > equivalent. So if your instructor took off points in the former
                        > case, he (or she) was simply wrong, but in the latter case he may
                        > have been justified.[/color]

                        Maybe the exam question was:

                        "Write a full expression which increases i by one, without using a
                        postincrement operator"...

                        Comment

                        Working...