Hi guys I find myself going back to the basics day by day. if the postfix operator precedence is much higher than the equality (==) operator, then in the following code: int i = 0; while( i++ == 0 ) System.out.print(i); System.out.print(i); shouldn't the expression be treated as follows? while( (i++) == 0 ) System.out.print(i) ?? BUT the the value is first compared and then incremented. The output is therefore '12'.
zarina mohammad
Ranch Hand
Joined: Jun 26, 2002
Posts: 104
posted
0
In the post increment i++ the value of i that is used in the calculation is the original value ,increment or decrement only occurs only after the expression is calculated. hence the output will be 1
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
A postfix operator will return the original value first before incrementing. In while( i++ == 0 ), you have ( 0 == 0) After this boolean statement executes, i is now equal to 1 and System.out.print(1). The while condition is now false so the next stament is executed, which is System.out.print(2); [ August 22, 2002: Message edited by: Anthony Villanueva ]
Vin Kris
Ranch Hand
Joined: Jun 17, 2002
Posts: 154
posted
0
Thanks Zarina & Anthony. I understand the reason for the output. But My question was regarding the operator precedence. The precedence of the postfix operator is higher than the equality operator. In this case, shouldn't the value be incremented before comparison? according to the precedence table? Thanks.
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
i is incremented before the comparison, but the comparison will still use the old value of i.