Tag: Programming MOC, C MOC
Increment ++ and Decrement — Operator as Prefix and Postfix
In programming (Java, C, C++, JavaScript etc.), the increment operator ++
increases the value of a variable by 1. Similarly, the decrement operator --
decreases the value of a variable by 1.
Simple enough till now. However, there is an important difference when these two operators are used as a prefix and a postfix.
++ and — operator as prefix and postfix
- If you use the
++
operator as a prefix like:++var
, the value of var is incremented by 1; then it returns the value. - If you use the
++
operator as a postfix like:var++
, the original value of var is returned first; then var is incremented by 1.
The --
operator works in a similar way to the ++
operator except --
decreases the value by 1.
Let’s see the use of ++
as prefixes and postfixes in C, C++, Java and JavaScript.
Example: C MOC Programming
Example: C++
Example: Java Programming
Example 4: JavaScript
The output of all these programs will be the same. Output 5 6