Postfix and Prefix increment and decrement
suggest changePostfix 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++)
{
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents