Menu
Coddy logo textTech

Increment/Decrement

Part of the Fundamentals section of Coddy's C++ journey — lesson 18 of 74.

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

The ++ and -- operators are special shortcuts that ONLY work for adding or subtracting 1. There is no ** or similar operator for multiplication.
For regular arithmetic operations (like multiplication, division, or adding/subtracting by amounts other than 1), you must use the assignment pattern:

int count = 5;
count = count + 3;  // Add 3: count is now 8
count = count * 2;  // Multiply by 2: count is now 16
count = count - 4;  // Subtract 4: count is now 12
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

The ++ and -- operators are special shortcuts that ONLY work for adding or subtracting 1. There is no ** or similar operator for multiplication.
For regular arithmetic operations (like multiplication, division, or adding/subtracting by amounts other than 1), you must use the assignment pattern:

int count = 5;
count = count + 3;  // Add 3: count is now 8
count = count * 2;  // Multiply by 2: count is now 16
count = count - 4;  // Subtract 4: count is now 12

Try it yourself

#include <iostream>

int main() {
    int count = 0;
    
    // Type your code below
    
    
    // Don\'t change the line below
    std::cout << "count = " << count;
    return 0;
}
quiz iconTest yourself

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

All lessons in Fundamentals