Increment or Decrement integers in apex
To increase or decrease numeric values in apex, the increment (++)
and decrement (--)
operators can be used. These operators increase or decrease the numeric value by one
.
The position of the operator (before or after the variable) determines when the value is updated during execution.
Operator | Usage | Example |
---|---|---|
Increment (++) | Increase the value by one and it can be used after the variable. |
|
Decrement (--) | Decrease the value by one and it can be used after the variable. |
|
Increment (++) | Increase the value by one and can be used before the variable. |
|
Decrement (--) | Decrease the value by one and can be used before the variable. |
|
The increment and decrement operators are commonly used in loops and conditional statements.
The following example shows how to increment a variable value before and after it is used.
Loading...
In the above example:
- The variables count, currentCount, items, newItems are
Integer
variables. - The operator
++
on count variable, increment the value of count by onebefore
it is assgined to currentCount. - The operator
++
on items variable, increment the value of count by oneafter
it is assgined to newItems.
Try It Yourself
Loading...