Reputation: 39
How are these two lines of statement different ?
a<=20? b=30: c= 30;
(a<=20)?b:c = 30;
If i give a value of a = 20 then
First line gives b = 20 , c =0 Second line gives b = 0 , c = 0
Not an assignment question.
Upvotes: 0
Views: 2765
Reputation: 7644
operator precedence:
this:
(a<=20)?b:c = 30;
is interpreted as:
(a<=20) ? (b) : (c=30) ;
you probably want
((a<=20)?b:c) = 30;
Upvotes: 1
Reputation: 172924
According to the rule of ternary conditional operator,
a<=20? b=30: c= 30;
is same as:
if (a <= 20) {
b = 30;
} else {
c = 30;
}
and (a<=20)?b:c = 30;
is same as:
if (a <= 20) {
b;
} else {
c = 30;
}
So the difference between case#1 and case#2 is whether b
get setted when the condition is true
.
Upvotes: 1