• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question ++ -- why it compile? use () and difference prefix and postfix

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
know about result it is result of prefix/postfix usage.
but why () not affect on operand, and it still compile?
for example
result is Compile failed; required: variable found: value
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prefix and postfix operators can only be used with a (number) variable. (a) is similar to a (which is a variable); (a=10) is not a variable, but an assignment so it's an illegal argument for ++ (or --) operation.

These compile:


And these don't:


Hope it helps!
 
Sharmili Rameshbabu
Greenhorn
Posts: 27
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am just curious to know how a=a--; or a=a++; works in the below code.



1.In case of b=a++; it is interpreted as b=a; and a=a+1; then a=(a)--; or a=a-- should be interpreted as a=a; a=a-1; and line1 should print 1. But why it is not happening? Please advise.
2.line2 also does not increment the value of a. Why?
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a=(a)--;
a = a, because a-- post decrement
a=a++;//line2 this expression is not affecting the value of a . why??
this same, but post increment.


in first expression
a=(a)--
last expression is assignment
1. (compute a-- and remeber it)
2. change value that in a.
3 assign valuer remembered at poin 1 to variable a.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmili Rameshbabu wrote:1.In case of b=a++; it is interpreted as b=a; and a=a+1; then a=(a)--; or a=a-- should be interpreted as a=a; a=a-1; and line1 should print 1. But why it is not happening? Please advise.
2.line2 also does not increment the value of a. Why?

In my explanation I use the post-increment, the same logic applies to post-decrement as well.

Let's say you have a statement like:This statement is similar to:

Now 2 examples to illustrate. First with a seperate variable b:
Why? Just apply the rule:

Now the 2nd example with just 1 variable a:
Why? Again we apply the rule:

Hope it helps!
Kind regards,
Roel
 
Sharmili Rameshbabu
Greenhorn
Posts: 27
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks Roel and Sergej for the response.

Now its very clear, i have understood where i was going wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic