Forums Register Login

Operator problem

+Pie Number of slices to send: Send
check this
int i = 9 ;
i = i++ ;
System.out.println(i) ;
output = 9
int i = 9 ;
i = ++i ;
System.out.println(i) ;

output = 10 Why?
Can we expact questions on these topics
- javadoc
- code in a html file for Applet
Mock exam by JohnJunt is covering above topics. Please guide.
+Pie Number of slices to send: Send
The answer to the operator part of your question lies in the sequence of events. With i = i++; I believe the sequence is:
1) Determine the value of the expression "i++", which is 9.
2) Increment i, making it 10.
3) Store the value of the expression (9) into i, putting it back to 9.
However, with i = ++i; the sequence is:
1) Increment i to 10.
2) Determine the value of the expression: 10.
3) Store the value into i, making it 10.
Pre-increment vs. post-increment (and -decrement) is an important distinction. Think of it this way: pre-increment first increments the value, then yields that new value, whereas post-increment yields as its value the old value and then "afterward" increments the variable.
+Pie Number of slices to send: Send
Thanx Bill
i got the funda. Appreciate your reply.
+Pie Number of slices to send: Send
Hi,
Amit i think this one we can explain in very easy way. Look it is very important when you get job in any company if employer
finds that you are making mistake in this code increment
you will be fired immediately from the job.
check this
int i = 9 ;
i = i++ ;
System.out.println(i) ;
output = 9
int i = 9 ;
i = ++i ;
System.out.println(i) ;

output = 10 Why?
Bcoz , i=i++ means i first assign its value to i then
it will start increments. So i==9

Second i=++i means i first be incremented then it will
assign its value to i. So i==10
Got it,
- Golam Newaz
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 655 times.
Similar Threads
why i=i++; doesn't get incremented
Jxam question
simple array question that I couldn't figure out!!
Output needed
about increment operaror
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 03:01:19.