what is problem with this conditional op ?

A

abir

I never fill comfortable with ?: and , operator.
There is problem with this statement
int x = 6, y = 20;
int p = 6;
p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

i get here x = 9, y = 62 (unexpected)
if i write
p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
62) );
i get x = 9, y = 61 (expected)

i even doubt if the second one is correct, though it gives correct
answer.
what is the problem here?

compiler MSVC 9 Express
thanks
abir
 
A

alfps

I never fill comfortable with  ?: and , operator.
There is problem with this statement
 int x = 6, y = 20;
    int p = 6;
    p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

i get here x = 9, y = 62 (unexpected)
if i write
 p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
62) );
i get x = 9, y  = 61 (expected)

i even doubt if the second one is correct, though it gives correct
answer.
what is the problem here?

compiler MSVC 9 Express
thanks
abir

This looks like a homework assignment, but hey, it's Christmas.

The parentheses you added should give you a (strong) clue.

In the C++ syntax the third expression of a conditional expression
can't be a comma expression.

It's akin to but not quite the same as precedence.

In C and C++ the expression syntax is designed to yield an illusion of
precedence, which mostly works well as a practical rule of thumb, but
this illusion breaks down for the combination of conditional and comma
operators, where you have to understand the syntax instead of
precedence.


Cheers & hth.,

- Alf
 
A

abir

This looks like a homework assignment, but hey, it's Christmas.
Nop ...
this is what i got from a software's complex pointer assignment
scheme
,where it was assigning all the wrong pointers.
Just tried to make it independently compilable program.
The parentheses you added should give you a (strong) clue.

In the C++ syntax the third expression of a conditional expression
can't be a comma expression.
but the compiler hadn't complained!
It's akin to but not quite the same as precedence.
how it finds a precedence which gives that result?
can you show the the way the compiler 'thinks' of correct precedence?
In C and C++ the expression syntax is designed to yield an illusion of
precedence, which mostly works well as a practical rule of thumb, but
this illusion breaks down for the combination of conditional and comma
operators, where you have to understand the syntax instead of
precedence.
i suspect that , is ignoring some of the assignments. but even in that
case
i am not sure how it correctly 'picks' the second condition, but
incorrectly
takes the last assignment.
i really fear those two operators. esp after Eric Niebler's article
Cheers & hth.,

- Alf

and happy Christmas :)
abir
 
A

alfps

Nop ...
 this is what i got from a software's complex pointer assignment
scheme
 ,where it was assigning all the wrong pointers.
 Just tried to make it independently compilable program.> The parentheses you added should give you a (strong) clue.


but the compiler hadn't complained!

Yeah. That means it parses OK. And that means you have a conditional
embedded in a comma expression, not a comma expression embedded in a
conditional.

It's akin to but not quite the same as precedence.

how it finds a precedence which gives that result?

It doesn't.

It just follows the syntax rules.

The syntax is defined as BNF rules.

can you show the the way the compiler 'thinks' of correct precedence?

Check out the syntax for 'expression'.

i suspect that , is ignoring some of the assignments. but even in that
case
i am not sure how it correctly 'picks' the second condition, but
incorrectly
takes the last assignment.
i really fear those two operators. esp after Eric Niebler's article
on ?:

Huh, unfamiliar to me.

:? is both very simple and very complex.

It's simple in cases like above, complex when the expressions aren't
of the same type.

and happy Christmas :)
abir

u2. Cheers. I'm off to Christmas dinner... :)

- Alf
 
P

Pascal J. Bourguignon

abir said:
I never fill comfortable with ?: and , operator.
There is problem with this statement
int x = 6, y = 20;
int p = 6;
p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

i get here x = 9, y = 62 (unexpected)
if i write
p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
62) );
i get x = 9, y = 61 (expected)

i even doubt if the second one is correct, though it gives correct
answer.
what is the problem here?

ENEP - Error, Not Enough Parentheses.

Unless you have the surhuman capability to memorize and regurgitate
flawlessly the grammar and operator precedence rules of C or C++, you
better fully parenthesize your code. Yes, C++ needs more parentheses
than Lisp (if you count braces, brackets and angulars), if you don't
like it, switch to lisp ;-)

By the way, another advantage of lisp with respect to these
syntactical elements, is that it doesn't distinguish expressions from
statements. So you can easily combine any expression:

C/C++ statement C/C++ expression lisp expression
---------------------- ------------------ ------------------------
if(...) ... else ... ... ? ... : ... (if ... ... ...)
.... ; ... ... , ... (progn ... ...)
{ vardecl... expr... } broken here (let (vardecl...) expr...)

etc, with a lot of other broken here boxes in the C/C++ columns.

(setf a (if (= 0 a)
(progn (print 'null) 42)
(let ((b (* 2 a))) (print b) (1- b))))

cannot be written in C/C++:

a=(0==a)
?(printf("null"),42)
:{ int b=2*a; printf("%d",b); return b-1;}; // no no...
 
A

acehreli

can you show the the way the compiler 'thinks' of correct precedence?

If the third expression cannot be a comma expression, then this is the
*almost* equivalent your statement:

if (p == 5) {
x = 8, y = 60;
} else {
if (p == 6) {
x = 9, y = 61;
} else {
x = 10;
}
}
// the last comma here...
y = 62;

I say "almost" because in your version, the value of the whole
statement is 62. In mine, there are two separate statements.

Ali
 
J

jason.cipriani

I never fill comfortable with  ?: and , operator.
There is problem with this statement
 int x = 6, y = 20;
    int p = 6;
    p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

i get here x = 9, y = 62 (unexpected)


Because it is being parsed as:

((p == 5) ? (x = 8, y = 60) : (((p == 6) ? (x = 9, y = 61)) : (x =
10))), (y = 62);

The "y = 62" is the second expression in a comma expression.

if i write
 p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
62) );
i get x = 9, y  = 61 (expected)

i even doubt if the second one is correct, though it gives correct
answer.
what is the problem here?


The root problem is the use of ?: when it may have been clearer to use
other constructs, e.g.:

if (p == 5) {
x = 8;
y = 60;
} else if (p == 6) {
x = 9;
y = 61;
} else {
x = 10;
y = 62;
}

That clearly states your intentions and does not suffer the same
tricky syntax issues.

Jason
 
J

James Kanze

I never fill comfortable with ?: and , operator.

There's no problem with ?:. You're problem is that you're using
the comma operator.
There is problem with this statement
int x = 6, y = 20;
int p = 6;
p == 5 ? x = 8, y = 60 : p == 6 ? x = 9, y = 61 : x = 10, y = 62;

Not surprising. The problem is precedence. Nominally, the
comma operator has lower precedence than ?:. Except that every
thing between the ? and the : must have higher precedence if the
code is to be compilable. (Otherwise, you'ld have a
sub-expression with a ? and no :.) Some good general rules:

-- Don't use the comma operator. Ever. It only causes
-- confusion.

-- Except for the simplest expressions, add parenthese around
the second and third operand of ?: (but most of the time,
the expressions should be simple enough it doesn't matter).

-- Format ?: on several lines, as if it were an if (or a
sequence of if/else if), e.g.

p == 5
? (x = 8, y = 60)
: p == 6
? (x = 9, y = 61)
: (x = 10)
, (y = 62) ;

(This corresponds to what you wrote. From your comments, I
gather that it isn't what you wanted.)
i get here x = 9, y = 62 (unexpected)

It's what you wrote.
if I write
p == 5 ? (x = 8, y = 60) : (p == 6 ? (x = 9, y = 61) : (x = 10, y =
62) );
i get x = 9, y = 61 (expected)
i even doubt if the second one is correct, though it gives
correct answer.

It's correct, although I'd skip the outer parathese around the p
== 6 ? expression. (It's basically an else if.) And format on
several lines.
what is the problem here?

Precedence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top