Differentiate between the following: Increment and Decrement operators
Advertisements
Question
Differentiate between the following:
Increment and Decrement operators
Advertisements
Solution
|
Increment Operator |
Decrement Operator |
|---|---|
|
1. The increment operator is a unary operator used to increase the value of a variable by 1. |
1. The decrement operator is a unary operator used to decrease the value of a variable by 1. |
|
2. The increment operator adds 1 to the current value of a variable. |
2. The decrement operator subtracts 1 from the current value of a variable. |
|
3. The increment operator is represented by two consecutive plus signs ( |
3. The decrement operator is represented by two consecutive minus signs ( |
|
4. The increment operator can be used in two forms: pre-increment ( |
4. The decrement operator can be used in two forms: pre-decrement ( |
|
5. In pre-increment, the value is increased before being used, whereas in post-increment, the original value is used before increasing it. |
5. In pre-decrement, the value is decreased before being used, whereas in post-decrement, the original value is used before decreasing it. |
|
6. The increment operator is commonly used to increase counters in loops and other repetitive operations. |
6. The decrement operator is commonly used to decrease counters in loops and other repetitive operations. |
|
7. Example: If |
7. Example: If |
