Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sergej Smoljanov
Ranch Hand
Posts: 472
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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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!
 
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: 472
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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.
 
Onion rings are vegetable donuts. Taste this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic