Increment (++)

Summary

Increases the value of a variable by one (1).

Syntax

variable++
++variable

Parameters

variable
Any numeric variable.

Description

++ increments the specified variable by one (1). While the operator can be used in a standalone statement, it can also be included anywhere the variable is used, such as function calls or assignment statements. In those cases, if the ++precedes the variable, it is known as the prefix increment operator and the variable is incremented before the statement is executed. If the ++ follows the variable, it is known as the postfix increment operator and the variable is incremented after the statement is executed.

Note that in a for statement, the update component is always executed after the body of the loop. Thus, ++i and i++ have the same effect in this case.

Examples

Standalone Use
1
2
3
4
5
import System;
 
int x = 2;
++x;
Console.log(x); // 3
Increment Before
1
2
3
4
5
6
7
import System;
 
int x = 2;
y = ++x;
 
Console.log(x); // 3
Console.log(y); // 3
Increment After
1
2
3
4
5
6
7
import System;
 
int x = 2;
y = x++;
 
Console.log(x); // 3
Console.log(y); // 2
for Statement
1
2
3
4
5
import System;
 
for (int i = 0; i < 5; ++i) {
    Console.log(i); // First iteration will be 0, last will be 4
}

See Also

Share

HTML | BBCode | Direct Link