Stats

Contributors: 1 Friday, July 22, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Postfix and Prefix increment and decrement

Download c# eBook

Example

Postfix increment X++ will add 1 to x

var x = 42;
x++;
Console.WriteLine(x); // 43

Postfix decrement X-- will subtract one

var x = 42
x--; 
Console.WriteLine(x); // 41

++x is called prefix increment it increments the value of x and then returns x while x++ returns the value of x and then increments

var x = 42;
Console.WriteLine(++x); // 43
System.out.println(x); // 43

while

var x = 42;
Console.WriteLine(x++); // 42
System.out.println(x); // 43

both are commonly used in for loop

for(int i = 0; i < 10; i++)
{
}