Thread: postfix and prefix...???

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    postfix and prefix...???

    just a quick question i hope....

    What is the difference between theses two "for" loops....


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

    and


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


    of course i am speaking of the last parameter....the postfix and prefix operators in this particular case only...

    thanx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What is the difference between theses two "for" loops....
    Programmer style. The increment is guaranteed to happen before the next iteration of the loop begins and after the previous iteration finishes, so prefix and postfix are exactly the same in this case.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well, there aren't differences in those loops per se.

    ++i and i++ differ in the return value. ++i returns the new value, i++ returns the old.

    If your increment/decrement is the only statement on a line, then it does not matter, but if you try and assign it to something, then it does.

    In general, use ++i unless you have some reason not to. Most of the time it does not matter.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thanx.....i was just wondering b/c i've seen different syntaxes among different applications...

    i know what it does in terms of assigment .. anyway, thank you....

    matheo917

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    Smile

    Prelude ----> why do u always quote other people's questions and statements... ??

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >why do u always quote other people's questions and statements... ??
    It's a habit I've gotten into because most threads ask more than one question. It's easier to understand what I'm talking about if I reiterate the question and then answer it. This becomes even more useful in very long threads with several conversations going on at once.

    -Prelude
    My best code is written with the delete key.

  7. #7
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    i read somewhere that since ++i always increments and i++ onlt increments if needed that the former s more eficient.

    M.R.

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I think the former is very slightly more efficient b/c it does not involve the use of a temporary.

    ++i resolves to:

    {
    i+=1;
    return i;
    }

    i++ resolves to:

    {
    int temp = i;
    i+=1;
    return temp;
    }

    Or, if i is equal to 2, then

    p = ++i;

    p and i equal three

    p = i++;

    p equals two, i equals three.

  9. #9
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    i stand corrected.
    thanks machine gun dude.

    M.R.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little question about postfix and prefix
    By firebird in forum C Programming
    Replies: 3
    Last Post: 05-08-2008, 07:27 AM
  2. Is prefix faster than postfix?
    By dwks in forum C Programming
    Replies: 6
    Last Post: 07-28-2005, 11:51 AM
  3. stacks postfix aww fooey
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2004, 10:00 AM
  4. postfix and prefix
    By drdodirty2002 in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 06:26 AM
  5. Data Structures / prefix and postfix
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-08-2001, 12:48 PM