Post: i++ vs. ++i
05-11-2013, 02:20 AM #1
Master Ro
I make food
(adsbygoogle = window.adsbygoogle || []).push({}); People have been asking me this question a lot now, so I thought I would make a thread about the difference of ++i and i++.

++i is a pre-increment and i++ is a post increment. What this means is ++i increments by one before you call it, and i++ increments after you call it.

You can basically think of ++i like this:

    
static void Main(string[] args)
{
int i = 0;


//i increments before this line, so before outputting i, we increment its value by one.
Console.WriteLine("{0}", ++i);
}


You can think of i++ like this:

    
static void Main(string[] args)
{
int i = 0;


Console.WriteLine("{0}", i++);
//i increments after this line, so only after outputting i, we increment its value by one.
}


So in the first case, the Console will output 1 because the value of i increases before we output it.
In the second case, the Console will output 0 because we only increment i after we output it.
When the function terminates, in both cases, i will have a value of 1.

When incrementing i in for loops, this doesn't make any difference (except maybe a small difference in program speed).

If you have any questions about this, feel free to post them here.

Smile

(adsbygoogle = window.adsbygoogle || []).push({});

The following 4 users say thank you to Master Ro for this useful post:

Devastation, K5‎‎, NeedaLifeSoon, TheUnexpected
05-11-2013, 03:12 AM #2
Mildly interesting but informative none the less I suppose regardless I think i++ has become more common, as it makes sense in more situations.

Computer numeric systems start from 0 instead of 1 so going i++ in a loop that is running x amount of times works rather than going ++i in a loop and (I may be backwards) but coming up short one.

I guess it's whatever you get used to first and from that point on you will modify your code to fit the situation.
05-11-2013, 03:50 AM #3
Master Ro
I make food
Originally posted by Killer1478 View Post
Mildly interesting but informative none the less I suppose regardless I think i++ has become more common, as it makes sense in more situations.

Computer numeric systems start from 0 instead of 1 so going i++ in a loop that is running x amount of times works rather than going ++i in a loop and (I may be backwards) but coming up short one.

I guess it's whatever you get used to first and from that point on you will modify your code to fit the situation.



It's really based on the situation you're in Winky Winky

For example, if I wanted to call an array by i incremented, I would use this:

    
arr[++i] = 0;


Instead of:

    
i++;
arr[i] = 0;


:p
05-11-2013, 04:39 AM #4
Pichu
RIP PICHU.
Edit: I'll write something up.
05-11-2013, 05:16 PM #5
Originally posted by Master
It's really based on the situation you're in Winky Winky

For example, if I wanted to call an array by i incremented, I would use this:

    
arr[++i] = 0;


Instead of:

    
i++;
arr[i] = 0;


:p


Ah yeah, it's adaptable either way though I guess if you were under real line constraints and cpu restrictions (I get bored and limit myself sometimes) it would help in shortening code but overall it's not something major.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo