C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The post-increment here begins with the value 0 and changes it to the value 1.
Also: You use any constant or other expression resulting in a numeric value in the increment statement.
Pre-increment: Pre-increment has 2 pluses before the identifier. The store opcode is executed before the load operation.
Info: With pre-increment, the variable value is changed and then you read it and evaluate it.
C# program that increments integers
using System;
class Program
{
static void Main()
{
//
// Shows how increments work.
//
int i = 0;
Console.WriteLine(i);
i++; // Add one
Console.WriteLine(i);
i += 2; // Add two
Console.WriteLine(i);
++i; // Add one
Console.WriteLine(i);
i += i; // Multiply by two
Console.WriteLine(i);
//
// Uses increments and assigns.
//
int v = 0;
v = i++; // Increment after value copy
Console.WriteLine(v);
Console.WriteLine(i);
v = ++i; // Increment before value copy
Console.WriteLine(v);
Console.WriteLine(i);
}
}
Output
0 -
1 Added one
3 Added two
4 Added one
8 Added itself
8 Value copy
9 Increment
10 Value copy
10 Increment
Preincrement:
++i;
Postincrement:
i++;
Note: You can sometimes combine assignments with preincrements. This can improve performance.
C# program that postincrements
class Program
{
static int _x;
static void Main()
{
// Add one.
_x++;
// Read in the value again.
int count = _x;
}
}
Here: We use preincrement to combine 2 statements into 1 statement. This is shorter code.
Program 2: C#
class Program
{
static int _x;
static void Main()
{
// Increment then assign.
int count = ++_x;
}
}
Version 1: We increment, and then load the value of the static int. Memory is accessed twice.
Version 2: We increment and load the field in the same expression. Memory is only accessed once.
Result: It is faster to combine the increment and read instructions into a single expression.
Important: For local variables, which are faster than fields, this may not have the same performance benefit.
C# program that times preincrement
using System;
using System.Diagnostics;
class Program
{
static int _test;
const int _max = 100000000;
static void Main()
{
_test = 0;
var s1 = Stopwatch.StartNew();
// Version 1: increment then load.
for (int i = 0; i < _max; i++)
{
_test++;
int z = _test;
if (z == 0)
{
return;
}
}
s1.Stop();
_test = 0;
// Version 2: increment and load in same expression.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
int z = ++_test;
if (z == 0)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
1.49 ns increment, then load
1.31 ns preincrement and load in single statement
Here: We start the value stored in the variable location with the integer 100. Then, the single decrement operator is applied.
And: This decreases the value by one. Finally we use the -= operator and use several operands with it.
C# program that uses decrement operators
using System;
class Program
{
static void Main()
{
int i = 100;
// Decrement by one.
i--;
Console.WriteLine(i);
// Decrement by two.
i -= 2;
Console.WriteLine(i);
// Decrement by negative one (add one).
i -= -1;
Console.WriteLine(i);
// Decrement by zero (do nothing).
i -= 0;
Console.WriteLine(i);
// Decrement by itself (results in zero).
i -= i;
Console.WriteLine(i);
}
}
Output
99
97
98
98
0
Order: When you use the post-decrement operator in an expression, the expression is evaluated before the decrement occurs.
And: When you use the pre-decrement operator, it is evaluated after. This difference is important.
C# program that uses two forms of decrement
using System;
class Program
{
static void Main()
{
int i = 5;
// This evaluates to true.
// ... The decrement occurs after the comparison.
if (i-- == 5)
{
Console.WriteLine(true);
}
// This evaluates to true.
// ... The decrement occurs before the comparison.
if (--i == 3)
{
Console.WriteLine(true);
}
}
}
Output
True
True