Home

Predecrement

Predecrement is a unary operator, typically written as --x, that decreases the value of its operand by one and yields the decremented value as the result of the expression. The operation also updates the value stored in the operand’s location. The operand must be a modifiable lvalue in most programming languages.

In use, predecrement reduces the variable before its value is used in the surrounding expression. For example,

Predecrement is contrasted with postdecrement, which is written as x--. Postdecrement yields the original value of

Common contexts for predecrement include loop counters, iterator advancement, and index or pointer calculations where the

Not all languages provide a predecrement operator. Some languages, such as Python, do not have -- and

int
a
=
5;
int
b
=
--a;
results
in
a
==
4
and
b
==
4.
The
side
effect
(the
decrement)
occurs
before
the
value
is
produced
for
the
assignment
or
further
computation.
x
and
then
decrements
it.
In
many
languages,
this
means
--x
and
x--
evaluate
to
different
results
even
though
both
modify
the
same
variable.
updated
value
is
needed
immediately.
instead
use
explicit
subtraction
assignments,
e.g.,
x
-=
1.
In
languages
that
do
support
it,
applying
predecrement
to
a
non-modifiable
expression
(such
as
a
constant)
is
invalid
and
will
typically
produce
a
compile-time
error.