Click here to Skip to main content
10,500,000 members (42,214 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ C
I've googled around and have not found documentation of this particular syntax.

Consider the following:

If m_iPage is indeed 7 then StartLight gets set to 1 and EndLight gets set to 4 as desired.
However if m_iPage is say, 0, the StartLight gets set to 0 but EndLight is set to 4, where I
really needed EndLight to be set to 0 also.

(m_iPage != 7) ? StartLight = m_iPage, EndLight = m_iPage : StartLight = 1, EndLight = 4; 

This is not correct?

Thanks for the help in advance
Posted 2-Mar-13 6:53am
Ron Anders10.1K
Edited 2-Mar-13 7:19am
v3
Comments
Philippe Mori 5-Mar-13 23:21pm
   
To understand why it does not works, you have to search information on comma operator and operator precedence instead of ternary operator. Since comma operator has less priority, last part of your expression is always executed as it is not part of the ternary expression.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

While ProgramFOX's answer is correct, it is precisely this sort of problem where it would be recommended to stick with an if/else statement. It will make the code easier to read and understand what you are doing, both for yourself when you come back to it later, as well as another developer if they have to take the work on.

Furthermore, whether you take that advice or not, you really would be better off having your condition in the positive form (since you are doing something in both cases) because "if this, do x, otherwise do y" is always easier logic to follow than "if not this, do x, otherwise do y".

I.e.
(m_iPage == 7) ? (StartLight = 1, EndLight = 4) : (StartLight = m_iPage, EndLight = m_iPage);


But I repeat, you really should look to change this, if possible, to:
if (m_iPage == 7)
{
   StartLight = 1;
   EndLight = 4;
}
else
{
   StartLight = m_iPage;
   EndLight = m_iPage;
}

Regards,
Ian.
  Permalink  
Comments
ProgramFOX 2-Mar-13 12:40pm
   
I agree, +5!
Ron Anders 2-Mar-13 12:45pm
   
But if then else is so much less sexy :-)
Philippe Mori 5-Mar-13 22:45pm
   
You should never use ternary operator for multiple statements but only for simple expressions and generally, you don't want to do any assignment on the right side of the ?.
Ian A Davidson 2-Mar-13 12:58pm
   
When you have been a software engineer as long as I have, you know the purpose isn't to write "sexy" code.
The best code is code that is maintainable.
There are places where the ternary operator is useful, but it would normally be for the simplest of cases. (One example, off the top of my head, may be to decide what value to pass to a base class in the constructor of an object). In your case, go with if/else. You would have saved yourself a lot of time already, and you'll save yourself a lot of pain later. (E.g. what if later you find you need to do something else when m_iPage is/is not 7 - you'd probably have to change to if/else anyway. Or what if you decide you also need to do something when m_iPage is 6, etc).
Regards,
Ian.
Jayanta Chatterjee 2-Mar-13 13:23pm
   
My 5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

You have to write parentheses:
(m_iPage != 7) ? (StartLight = m_iPage, EndLight = m_iPage) : (StartLight = 1, EndLight = 4);
Hope this helps.
  Permalink  
Comments
Ron Anders 2-Mar-13 12:20pm
   
Yep, that did it. Thanks ProgramFOX!
ProgramFOX 2-Mar-13 12:21pm
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160212.1 | Last Updated 2 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100