Win a copy of Design for the Mind this week in the Design forum!

# SCJP : query regarding increment/decrement operator.

ankur trapasiya
Ranch Hand
Posts: 160
Hello... I am preparing for SCJP and i found one difficulty regarding operators.

for e.g. in a compound statement like a[i++][++i] or statements like this .... (assume i as integer).

so i am confused about that when actually value of i will be incremented here and how this entire statement will be evaluated by the compiler. In Kathy sierra and Bert Bates book there is very less details are given . can any one prefer any material to understand this or can make me understand this topic ?

Matthew Brown
Bartender
Posts: 4566
8
• 1
In this sort of case I'd suggest trying it to see what happens. Try this (not checked):

Ahmed Attyah
Greenhorn
Posts: 4
i++ : Simply i++ means it reads the value then increments it which means it will return the original value but "i" itself incremented

++i : it increments i then reads it , it will retrun value of increment

so, both increment the value of i but the difference is in return value , i++ returns original value , ++i return value after increment.

Note: i don't think this is the appropriate forum for SCJP Questions

ankur trapasiya
Ranch Hand
Posts: 160
dear ahmed. i know pre-increment and post increment operations pretty good but the thing i wanted to know was that the order in which these operations are carried out..

e.g. int j =12; i= ++j + j++ * ++j ;

something like this. that's why i asked this question to know the flow this execution...

Ranch Hand
Posts: 87
int j =12; i= ++j + j++ * ++j ;
the order is from left to right:
1- First all your j's will be inialized as follws i = 13 + 13 * 15. and you end up with j=15
2- the arithmatic operation carried out as i = 208

(why all these smiles to someone who want to let us know more)

ankur trapasiya
Ranch Hand
Posts: 160
hey Imad...!!! Thanks a lot... my doubt got cleared and please dont mind putting similes in my lines is my habit .