Hello everybody!

I'm learning Java programming and have a really, really dumb question to ask.

When writing a line with a counter, such as:

for (var count = 0; count < aString.length; count ++)

what does the ++ do? I've seen it in lots of code and it is mentioned in my book, but what does it actually do?

Cheers!

Recommended Answers

All 3 Replies

It's called "incrementation." It's something that has been in programming for a very long time. Even as low level as assembly language, has a special opcode for incrementing variables (INC in asm). This is the process of simply adding one. So, if the value of count is 1, then after the count++ the variable count becomes 2. Higher level languages, such as BASIC (in all forms that I know of) do not use this method. You have to explicitly say: count = count + 1. Also, WHERE you put the ++ becomes important in some languages also. If you put the ++ after (count++) the variable name, then it adds 1 to the variable AFTER whatever condition is tested. If you were to put ++ BEFORE the variable (++count) then it would increment (add one) to the variable BEFORE the condition is tested. Hope that wild explanation has helped a little.

It's called "incrementation." It's something that has been in programming for a very long time. Even as low level as assembly language, has a special opcode for incrementing variables (INC in asm). This is the process of simply adding one. So, if the value of count is 1, then after the count++ the variable count becomes 2. Higher level languages, such as BASIC (in all forms that I know of) do not use this method. You have to explicitly say: count = count + 1. Also, WHERE you put the ++ becomes important in some languages also. If you put the ++ after (count++) the variable name, then it adds 1 to the variable AFTER whatever condition is tested. If you were to put ++ BEFORE the variable (++count) then it would increment (add one) to the variable BEFORE the condition is tested. Hope that wild explanation has helped a little.

This helps a lot, thanks. Just to maintain the dumb question ideal, can I use count = count + 1, and it does the same job?

Cheers!

The Simple Answer: Yes.

for (i=0; i<100; i = i +1) {
     document.writeln(i + "<BR>");
}

Works (tested in firefox) just fine. The thing with that though... is that if you are concerned about programming speed and power, using var = var + 1 is NOT the way to go. As a matter of programming ethics, and as a matter code effeciency (on a very low level, it takes more time to process i = i + 1, than it does to process i++, but even at a low level, a bunch of i = i + 1's can add up). The best way to add one to a variable is to use ++. The only reason you shouldn't use ++, is if the programming language you are using doesn't support it (like visual basic).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.