Bear Bibeault wrote:Option A because it's the convention.
O Shea wrote:I think Option B
Liutauras Vilda wrote:Any reasoning?
As long as you don't expect anybody else to read it, that is. There are reasons for following conventions which Paul C has already told you about.
O Shea wrote:[. . . to try something different.
Tim Holloway wrote:
Knute Snortum wrote:
Tim Holloway wrote:
I believe that should be and also the other for loop.
Tim Holloway wrote:I spent so much time trying not to screw up my calculations...
I am afraid that is incorrect. The instruction inside the () has to be completed before the print instruction is executed, but you cannot see the value of i. The value of the whole expression i++ is equal to the old value of i. The details are in the Java® Language Specification (=JLS):-
Daniel Bernardy wrote:the difference between ++i and i++ is whether one is added to i before the statement is used . . . or after the statement is done . . .
//returns 0 because it adds one to i after the print line is executed
. . .
The new value of i becomes visible later whenever you use i again.
That JLS Section wrote:The value of the postfix increment expression is the value of the variable before the new value is stored.
Campbell Ritchie wrote:Some people say there is a performance enhancement in Java® too if you use i++, but what is one clock cycle between friends?
Campbell Ritchie wrote:Thank you. I seem to have got my previous post wring; I meant to say they say ++i is faster. Sorry.
Mickey Loveless wrote:Also, depending on your code using "++i" could return an error depending on what you are using your loop for.
Mickey Loveless wrote:Both work its just convention wise "i++" is more acceptable. Also, depending on your code using "++i" could return an error depending on what you are using your loop for.
Andy Frels wrote:I would prefer option B (++i) simply because it makes sure the value is where it should be when running the loop, although personally I don't think it matters when using a for loop.
Roger Williamson wrote:I++, because everything will have to be evaluated before you add on I.
I am afraid that isn't correct. As Junilu has hinted, the increment clause of the for statement header is executed as if it were a separate statement. The loop is executed rather like this:-
Roger Williamson wrote:. . . everything will have to be evaluated before you add on I.
Campbell Ritchie wrote:As Junilu has hinted
Junilu Lacar wrote:
Again, there is absolutely NO functional difference between these two as far as controlling the loop iteration and increment is concerned:
When you have for-loop code in this form, the former is preferable because it is the conventional way to write it and it's what most people expect to see.
Ryan McGuire wrote:
- In the few cases where the value of the expression is used, it's more likely (in my experience) that you want the new, incremented value of i instead of the old value.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your databasehttps://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database