Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Programming fundamentals: What does ++ mean in coding?

PS author and software engineer Josh Cummings explains what the operator means in programming, how to use it, and when to use alternative methods.

Aug 15, 2024 • 8 Minute Read

Please set an alt value for this image...
  • Software Development
  • Guides

So, you’re starting out programming, and you’re wondering what the ++ people put next to their variables mean. Does it add extra charge to your variable, like a battery? Does it give said variable a more positive outlook? Perhaps it’s like getting an A++ on a test, showing the variable is doing a really good job?

In this article, we’ll talk about what ++ really means when coding, how you can use it, and even dive into alternatives to this operator you might want to use when it comes to concurrency. 


Table of contents


The ++ in programming, explained

++ is coding shorthand used to increase the value of a numeric datatype by 1. So, if you had a variable called candles with a value of 16, you could write candles++, and candles would then have a value of 17. This is way easier than writing out candles = candles + 1.

In programming, this is known as incrementing a variable, which is just slang for “increase that value by a set amount.” By default, this increases the numeric value by one.

So, why use the ++ operator at all if you can just write it out the second way? 

  • It can make your coding more concise
  • You only need to reference the variable once, not twice (++ is known as a unary operator, which means you only need one value for it to be a valid expression)

Referencing a variable multiple times when you don’t need to is redundant, and as any programmer knows, redundancy is something that should always be avoided. Also, the more you reference the variable, the more chances you’ll type it incorrectly, increasing the chance of error.

So, what if you wanted to decrease a variable by one instead of adding to it? Keep reading!

What does - - mean in coding?

-- is used to decrease the value of a numeric datatype by one. It’s used identically to the ++ operator. For example, if burgers had a value of 5 and you wanted to decrease it by one—presumably by eating said burger—you could type burgers-- and the value would then be 4 (You’d also be a lot fuller for it.)

What languages use the ++ operator?

The ++ is native to C-based languages like C, C++, and C#. Java, Javascript, and PHP also support the ++ operator. While Swift used to support both the ++ and -- operators, this has unfortunately been depreciated.

What languages don’t use the ++ operator?

As of the time of writing, languages like Python, Ruby, Lua, Haskell, Rust, and Elixir do not support the ++ operator. This can be because of their design philosophy—not everyone finds the ++ operator simple to understand or more readable—or because they’re functional languages with immutable variables, so incrementing doesn’t make sense.

The difference between ++ and + in coding

One of the important things to remember is that + is an arithmetic operator, and ++ is both an arithmetic and assignment operator. What does that mean? Well, the first will calculate an equation for you, while the second will calculate the result and assign it to a variable that contains the new value.

To help illustrate the difference, consider the following code, where candles equals 10.

      int items = candles + 1;
    

In this scenario, the value of candles will still be 10, and items will now equal 11. Why not? Because + is only an arithmetic operator. Just like in math class, adding a and b together doesn’t change the values of a and b. 

However, some things aren’t like math class. For example, in programming, you can assign that new value to a new variable or even update the assignment of an existing variable. That makes = an assignment operator.

As mentioned earlier, ++ is special because it is both an arithmetic operator and an assignment operator. This means that candles++ adds 1 to candles and then updates its assignment to contain that new value.

Can I use ++ to increment by two or more instead of one?

No. For this scenario, you’d use the += operator (or -= if you want to decrease the number). For example, if you typed burgers += 2, this would increment the number of burgers by two. You can change the number to adjust the amount you want to increment the value.

Best use cases for ++ in coding

1. Making your loops more efficient

If you’ve seen a loop, you’ll usually see the ++ operator. Loops are a way to repeat certain tasks in code without writing it out over and over again. In Java for example, this might look like this:

      String[] names = { “John”, “Paul”, “George”, “Ringo” };
for (int index = 0; index < names.length; index++) {
    System.out.println(names[index]);
}
    

This loop will print out each name from names, one at a time, like so:

      John
Paul
George
Ringo
    

The syntax for a for loop can feel a bit intense at first. But since it is likely the first place you’ll be exposed to ++ in programming, let’s work through it. As you can see, this for loop has three expressions in its declaration, separated by semicolons:

  • The first is a variable declaration int index = 0. It serves to declare where the loop starts.

  • The second is a variable comparison index < name.length. It serves to declare when the loop should stop.

  • The third expression is an incremental expression index++. It serves to update index on each pass through the loop

Remember that variable = variable + 1 and variable++ are interchangeable. That means we could hypothetically write the same for loop this way:

      String[] names = { “John”, “Paul”, “George”, “Ringo” };
for (int index = 0; index < names.length; index = index + 1) {
    System.out.println(names[index]);
}
    

Because this kind of loop was quite common in early programming, the ++ operator saved a lot of typing. It also helped to make the loop more readable.

A modern alternative: Enhanced for loop

Some languages have enhanced for loop syntax which manage this counter internally. For example, in Java you can write:

      String[] names = { “John”, “Paul”, “George”, “Ringo” };
for (String name : names) {
    System.out.println(name);
}
    

This is a fine simplification when you don’t need to know that the current loop index is.

2. Counting your data

Another circumstance where ++ is common is when tracking data. Many applications want to track performance and traffic metrics for sample. They might have a method similar to this one:

      private int visitorCount;
// …
public void visitorVisitsWebsite() {
    this.visitorCount++;
}
    

This method imagines that there is a class, say VisitorStatistics, that keeps track of several counters about visitors. In this case, we could do:

      public void visitorVisitsWebsite() {
    this.visitorCount = this.visitorCount + 1;
}
    

Though using ++ is more succinct.

Using ++ as a prefix or postfix in coding

You can use the ++ operator in two forms: ++variable (prefix operator) and variable++ (postfix operator). The prefix (++variable) increases the value before the variable is used in an expression, while postfix (variable++) increases the value after the expression is used.

What’s the difference? Well, with the prefix version, you might want to increment a value before you assign it to something else. For example:

Prefix

      int x = 5; 
int y = ++x; 

    

In this case, x is incremented to 6, then y is set to 6.

Postfix

      int x = 5; 
int y = x++; 
    

In this case, y is set to 5, then x is incremented to 6.

When might this be useful? Well, you might have a section of code like this, where order of operations becomes important:

      int a = 32;
int b = a++;
// a is now 33, and b is now 32.
int c = ++a;
// a is now 34, and c is now 34.

    

  

Tricky ++ expressions to avoid

Because ++ can be quite tricky to read and wrap your head around, you should avoid using it as part of a larger expression. Leave it on its own line as much as possible. Consider how much more readable the following is, compared to our example from earlier:

      int a = 32;
int b = a;
a++;
// a is now 33, and b is now 32.
a++;
int c = a;
// a is now 34, and c is now 34.
    

Instead of embedding ++ in a larger expression, now it’s clearer that b is 32 and c is 34.

Issues with ++ and concurrency

Concurrency is the ability of a system to handle multiple tasks or operation simultaneously, without necessarily doing one task before the other. Unfortunately, the ++ operator can cause issues here, because when it is written to byte code it counts as more than one CPU instruction

This concept might be a bit mind bending. What this means is it two CPUs call the same instruction at roughly the same time, you can lose some of these ++ increment commands. This is sometimes called the “ABA problem” in concurrent programming. 

To demonstrate, consider what might happen with visitorCount in the following circumstance:

Visitor count

Thread A

Thread B

32

increment (33)

 

33

store (33)

 
  

increment (34)

34

 

store (34)

 

All of this looks fine, right? But what if this happened with both threads?

 

Visitor count

Thread A

Thread B

32

increment (33)

 

 

 

increment (33)

33 

store (33)

 

store (33)

 

 

In this case, both threads read visitorCount before one of them stored their updated value. This is called a lost update.

A solution to ++ and concurrency in Java: AtomicInteger

If you’re using Java, it has something better for multi-threaded applications called AtomicInteger. If you change your code to:

      private AtomicInteger visitorCount = new AtomicInteger(0);
// …
public void visitorVisitsWebsite() {
    this.visitorCount.increment();
}
    

…This ensures that the arithmetic operation and the assignment operation can’t be split up.

Conclusion

++ is quite common in older-style coding for loops, parsers, and counters. If you decide to use it, keep it simple. Make sure you give your increment its own line and preference postfix notation. If you can, replace it with a modern counterpart like an enhanced for loop or AtomicInteger.

Congratulations, you now know all about the ++ operator, and you’re more knowledgeable than you were at the start of this article. You can increment your knowledge by one—give yourself an A++!


Further learning resources

Want to master the basics of programming by watching a series of professionally authored videos instead of Googling for explainer articles? Pluralsight offers a language-agnostic course on the basics of programming by Simon Allardice that you can check out, which covers topics such as operators, loops, data structures, and all the things you need to know. 

If you’ve got a specific language you want to learn, below are some learning pathways you might be interested in:

Josh Cummings

Josh C.

Like many software craftsmen, Josh eats, sleeps, and dreams in code. He codes for fun, and his kids code for fun! Right now, Josh works as a full-time committer on Spring Security and loves every minute. Application Security holds a special place in his heart, a place diametrically opposed to and cosmically distant from his unending hatred for checked exceptions.

More about this author