1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple but many of the people don't aware of it

Discussion in 'C, C++, C#' started by seoways, Jul 19, 2013.

  1. seoways

    seoways Jr. VIP Jr. VIP Premium Member UnGagged Attendee

    Joined:
    Dec 19, 2009
    Messages:
    4,649
    Likes Received:
    724
    Location:
    Behind you!
    Difference between i++ and ++i

    Most of us struggle with these two while finding the output of C programs.

    i++ works follows:

    First the value of i will be used and the value will be incremented
    Code:
    i=1;
    j=i++;
    
    ans:i=2,j=1.

    In case of ++i:

    The value of i will be increased first then it'll be used.

    Code:
    i=1;
    j=++i;
    ans:i=2,j=2.

    Hope above information will be helpful for the beginners.
     
    Last edited: Jul 19, 2013
  2. TheGardenGnome

    TheGardenGnome Junior Member

    Joined:
    May 22, 2013
    Messages:
    148
    Likes Received:
    24
    Location:
    Singapore
    Thank you, starting to get into coding myself and this clarified some things for me.

    Cheers :D
     
  3. _Biju_

    _Biju_ Junior Member

    Joined:
    Nov 18, 2010
    Messages:
    171
    Likes Received:
    67
    Location:
    Here
    Home Page:
    ++i is not used as much as i++ and why should we use it ?
    instead of writing some_func(++i); we can just write it separately on two lines:

    Code:
    i++;
    some_func(i);
    much more elegant, that is unless you're competing for 25 lines of code contests.

    here's how you can use a boolean var to toggle itself:

    Code:
    bool temp = false;
    temp = !temp; // now it's true
    temp = !temp; // now it's false
    usefull for many things so you won't have to compare it's initial state.
     
    Last edited: Jul 19, 2013
  4. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    500
    Likes Received:
    257
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
    that function would be pointless, imho

     
  5. Orbit143

    Orbit143 Senior Member

    Joined:
    Aug 8, 2010
    Messages:
    893
    Likes Received:
    588
    Location:
    /home
    This is totally basic stuff, I never used it though...
     
  6. seoways

    seoways Jr. VIP Jr. VIP Premium Member UnGagged Attendee

    Joined:
    Dec 19, 2009
    Messages:
    4,649
    Likes Received:
    724
    Location:
    Behind you!
    why we should't use ++i
    it reduces the length of the program.
    it is an efficient way of programming.
     
  7. officialgt

    officialgt Regular Member

    Joined:
    May 27, 2013
    Messages:
    207
    Likes Received:
    24
    c++ programming is cool
     
  8. dexxtaa

    dexxtaa Regular Member

    Joined:
    Oct 4, 2012
    Messages:
    394
    Likes Received:
    178
    I used to always confuse those 2 in my java classes :confused:

    thanks for reminding me!
     
  9. seoways

    seoways Jr. VIP Jr. VIP Premium Member UnGagged Attendee

    Joined:
    Dec 19, 2009
    Messages:
    4,649
    Likes Received:
    724
    Location:
    Behind you!
    But, in java the precedence will be from left to right. don't be confused with it.
     
    Last edited: Jul 19, 2013
  10. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    Pre vs. post decrement is more a matter of style in the trivial case. However, in C++ if the post decrement operator is overloaded for a user-defined object, a copy of the object will be made before the operator is evaluated. This is not an issue for int or POD sized objects, but if your class is on the order of kilobytes or megabytes, using post-decrement will cause a significant performance penalty. This is especially true if your object does not fit into d-cache or is larger than the page size of the TLB.
     
  11. blackhat777

    blackhat777 Elite Member

    Joined:
    Jun 25, 2011
    Messages:
    1,779
    Likes Received:
    653
    Ahh.. Good old days when I was more into programming. Now, i'm totally into Data warehousing.
     
  12. cyberbrute

    cyberbrute Registered Member

    Joined:
    Jul 29, 2013
    Messages:
    72
    Likes Received:
    47
    Occupation:
    Student
    Location:
    Pakistan
    I am always stuck in my exams with Pre and Post orders, you cleared my concept.

    Thank you,
    Waseem
     
  13. seoways

    seoways Jr. VIP Jr. VIP Premium Member UnGagged Attendee

    Joined:
    Dec 19, 2009
    Messages:
    4,649
    Likes Received:
    724
    Location:
    Behind you!
    Happy with the great response mates!!
     
  14. buckybrendan

    buckybrendan Registered Member

    Joined:
    Aug 6, 2013
    Messages:
    64
    Likes Received:
    72
    Occupation:
    Web Developer
    Location:
    Ireland
    in exams i always get this wrong but while coding i very rarely mix them up. it is a basic part of coding its just that many ppl such as myself over look the basics but they are fundamental
     
  15. nopme88

    nopme88 Registered Member

    Joined:
    Jul 30, 2013
    Messages:
    50
    Likes Received:
    7
    Occupation:
    Freelancer
    Yep. I remember I have confused over them a great deal in the beginning of my C classes..