Decrement (--)

Summary

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

Syntax

variable--
--variable

Parameters

variable
Any numeric variable.

Description

-- decrements 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 decrement operator and the variable is decremented before the statement is executed. If the -- follows the variable, it is known as the postfix decrement operator and the variable is decremented 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); // 1
Decrement Before
1
2
3
4
5
6
import System;
 
int x = 2;
y = --x;
Console.log(x); // 1
Console.log(y); // 1
Decrement After
1
2
3
4
5
6
import System;
 
int x = 2;
y = x--;
Console.log(x); // 1
Console.log(y); // 2
for Statement
1
2
3
4
5
import System;
 
for (int i = 5; i > 0; --i) {
    Console.log(i); // First iteration will be 4, last will be 0
}

See Also

Share

HTML | BBCode | Direct Link