🙋 Need help? Ask an expert now!

The decrement operator functions similarly to the increment operator. The only difference is that the decrement operator decreases a value by one. This means we can write varOne--; instead of writing varOne = varOne - 1;. Let's take a look at these decrementing variables in the code below.

package exlcode;

public class DecrementExample {

  public static int exampleVariableOne = 10;

  public static void main(String[] args) {
    // Both statements subtract 1 from exampleVariableOne

Like the increment operator, there are two different ways to use the decrement operator called prefix and postfix decrement. The prefix decrement looks like --variablename; while the postfix increment looks like variablename--;. Both of these subtract one to the value in the variable. --varOne; decreases the value of varOne by one before returning the value of varOne. On the other hand, varOne--; returns the original value of varOne before decreasing the value of varOne by one. It is unlikely that we will print these results, but it is essential to know the difference between the functionality as it is commonly used in loops. Additionally, these operators will reduce the amount of code the programmer has to write.

Edit Me on GitHub!

Application Question

Consider the following code segment:

int varOne = 0;
            int varTwo = 10;
            varOne = --varTwo;
            System.out.println("varOne: " + varOne + " varTwo: " + varTwo);

What is printed as a result of executing this code segment?