![]() |
VOOZH | about |
Increment operator is used to increment a value by 1. There are two varieties of increment operator:
Example
Decrement operator is used for decrementing the value by 1. There are two varieties of decrement operators.
Example
Now let us do Interesting facts about Increment and Decrement operators:
Let us discuss these 4 facts as listed above and do implement them as follows:
Fact 1: Can be applied to variables only
We can apply ++ and -- operator only for variables but not for the constant values. If we are trying to apply ++ and -- operator on the constant value then we will get a compile-time error which we will be seeing in example 1B after the below example as follows:
Example 1:
11
Example 2:
Output:
Fact 2: Nesting of both ++ and -- operators are not allowed
Example
Output:
Fact 3:Final variables canβt apply increment and decrement operator
The increment and decrement operators can not be applied to final variables because of the simple reason that their value can not be changed.
Example
Output:
Fact 4: Increment and Decrement Operators can not be applied to boolean
We can apply ++ and -- operators for all primitive data types except Boolean type as it only has true and false which even sounds impractical.
Example