Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

increment operators

 
Angela lewis
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I fail to understand the 2 + signs before k in 1.
 
Shilpi M Ag
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can very well remove one of the +.. It is there to confuse us.. and simply means that k is positive.

You can change it to the following to see the difference
 
black night
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi angeela
i am totally agree with silpi , its only to cofuse , + + or even hundred + means the same one + . Here first + for addition and second + for sign notation


code:

public class TestClass
{
public static void main (String args[ ] )
{
int k = 1;
int i = ++k + k++ + + k ;
System.out.print(i+ " "+ k) ;
}
}
/*
++k = 1 + 1 =2
k++ = 2+1=3
+k = +3


i = ++k(2) + k++(2) + +k(3);
i = 2 + 2 + 3 = 7
*/



The solution might be confusing.If is there any mistake then please tell me . cia
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic