Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is problem with this conditional op ?

Reply
Thread Tools

what is problem with this conditional op ?

 
 
abir
Guest
Posts: n/a
 
      12-24-2008
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
 
Reply With Quote
 
 
 
 
alfps
Guest
Posts: n/a
 
      12-24-2008
On 24 Des, 14:32, abir <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
abir
Guest
Posts: n/a
 
      12-24-2008
On Dec 24, 6:59 pm, alfps <(E-Mail Removed)> wrote:
> On 24 Des, 14:32, abir <(E-Mail Removed)> wrote:
>
>
>
> > 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.
>

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
on ?:
> Cheers & hth.,
>
> - Alf


and happy Christmas
abir
 
Reply With Quote
 
alfps
Guest
Posts: n/a
 
      12-24-2008
On 24 Des, 15:18, abir <(E-Mail Removed)> wrote:
> On Dec 24, 6:59 pm, alfps <(E-Mail Removed)> wrote:
>
> > On 24 Des, 14:32, abir <(E-Mail Removed)> wrote:

>
> > > 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.

>
> 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!


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'.


> > 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
> 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
 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      12-24-2008
abir <(E-Mail Removed)> writes:

> 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...



--
__Pascal Bourguignon__
 
Reply With Quote
 
acehreli@gmail.com
Guest
Posts: n/a
 
      12-24-2008
On Dec 24, 6:18*am, abir <(E-Mail Removed)> wrote:
> On Dec 24, 6:59 pm, alfps <(E-Mail Removed)> wrote:
>
> > On 24 Des, 14:32, abir <(E-Mail Removed)> wrote:


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


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


> 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
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      12-25-2008
On Dec 24, 8:32*am, abir <(E-Mail Removed)> wrote:
> 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


> compiler MSVC 9 Express
> thanks
> abir


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-25-2008
On Dec 24, 2:32 pm, abir <(E-Mail Removed)> wrote:
> 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Problem evaluating conditional operator Richard Tierney C++ 3 07-27-2004 09:23 AM
Re: very strange conditional problem J. Romano Perl 0 02-21-2004 04:55 PM
Re: very strange conditional problem J. Romano Perl 0 02-21-2004 04:14 PM
Conditional operator problem Mahesh Tomar C Programming 6 07-22-2003 03:50 PM



Advertisments