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

unary operator

 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q:An unary operator operates on a single value
1.True
2.False
The answer is given as False,bcos + and -,can take two values.
Since,+ and - can be used both as unary and binary operator,what would be the best possible explanation in this context?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same symbols are used for unary operators:
+variable
-variable
as for additive operators:
var1 + var2
var1 - var2
I disagree with the answer. Because additive operators are not unary ones. Though they share the same symbols.
 
geetha nagarajan
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When '+' or '-' is used as a unary operator,it acts only on a single value,right.
So,the answer should be true.
There was another question about operands :
Q:All operands r evaluated left to right.
1.true
2.false.
The answer is given as true.Will the precedence and associativity rules not apply?
Pl.clarify.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the operands are evaluated, the operations are performed based on the precedence of the operators (and parentheses). Associativity plays a rol when considering operators with the same precedence.
Please see an example
JLS deals with Evaluation Order
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic