Menu
Coddy logo textTech

Increment/Decrement

Part of the Fundamentals section of Coddy's Java journey — lesson 17 of 73.

Increment and decrement operators are used to increase or decrease the value of a variable by 1. These operators are widely used in programming, especially in loops and counters.

The increment operator is represented by two plus signs ++, and the decrement operator is represented by two minus signs --.

For example, to increment a variable named count, you can use the increment operator like this:

int count = 5;
count++; // count is now 6

Similarly, to decrement a variable named value, you can use the decrement operator like this:

int value = 10;
value--; // value is now 9

This is equivalent to this:

count = count + 1 // increment by 1
count = count - 1 // decrement by 1
challenge icon

Challenge

Beginner

You are given a code with initialization of count. (Don't delete this line!)

Your task is to add the following operations, in this order:

  1. Use the increment operator (++) four times to add 4 to count
  2. Use the multiplication operator (*) to multiply count by 2
  3. Use the decrement operator (--) once to subtract 1 from count

Cheat sheet

Increment and decrement operators are used to increase or decrease the value of a variable by 1.

The increment operator ++ increases a variable by 1:

int count = 5;
count++; // count is now 6

The decrement operator -- decreases a variable by 1:

int value = 10;
value--; // value is now 9

This is equivalent to this:

count = count + 1 // increment by 1
count = count - 1 // decrement by 1

Try it yourself

public class Main {
    public static void main(String[] args) {
        int count = 0;
        
        // Type your code below
        
        
        // Don't change the line below
        System.out.println("count = " + count);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals