944,169 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1338
  • C++ RSS
Oct 29th, 2007
0

please need help .. with swith and conditional operator?

Expand Post »
hi ..

please can anyone help me with this .. i need it today

i wanna to do swith with this .. can i or not?

if ( x>y )
cout<< " x is bigger ";
else
cout<< " y is bigger ";

--
and i wanna to do conditional operator with more than 2 expressions .. can i do that?
and is that write or wrong?

grade == 'A' ? "ex" : grade == 'B' ? "v.g" : grade == 'c' ? "g" : undefined letter

thanx
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
DREAMER546 is offline Offline
27 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

Both should work.
Personally, I prefer to use lots of parentheses to make it obvious (both to myself and to the compiler) what exactly I mean to say:
C++ Syntax (Toggle Plain Text)
  1. cout << (
  2. (grade == 'A') ? "excellent" : (
  3. (grade == 'B') ? "very good" : (
  4. (grade == 'c') ? "good" : "not so good at all" )))
  5. << endl;
Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

switch( x>y ) { ... } ?
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

Duoas .. thanx alot ..

---
twomers .. i don't understand ?

please any one answer me in q1
Reputation Points: 10
Solved Threads: 0
Light Poster
DREAMER546 is offline Offline
27 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

Sure. Boolean is an ordinal type.
C++ Syntax (Toggle Plain Text)
  1. switch (x < y) {
  2. case true:
  3. // hmm, x < y
  4. break;
  5. case false:
  6. // wait, x >= y
  7. }


[EDIT] man, too slow again...

No prob DREAMER546 :-)
Last edited by Duoas; Oct 29th, 2007 at 3:51 pm.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

thanx aloot really .. because i read in the book the swith just take a constant integral expression i were think when we put < is wrong ..
Reputation Points: 10
Solved Threads: 0
Light Poster
DREAMER546 is offline Offline
27 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

All this: "I wanna do this by using that is nonsense."

As long as you use something that you understand it doesn't matter honey!
Last edited by iamthwee; Oct 29th, 2007 at 4:23 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

yeah i know that we show use what we understand .. but there's questions that request to solve it by a define way .. so that i'm asking for another way ..
Reputation Points: 10
Solved Threads: 0
Light Poster
DREAMER546 is offline Offline
27 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

Yoinks, iamthwee, at least she is asking to learn...

This is an expression: x > y. It evaluates to an integral (aka ordinal) value (specifically, a bool), which is what the switch statement takes. So when you say
switch (something) {
as long as something evaluates to an integral expression then the switch will work just fine.

Nobody uses switch for boolean expressions because it is faster and easier to read just to use an if. The if, btw, also takes an integral expression. If zero, it is understood as false. If non-zero, it is understood as true.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 29th, 2007
0

Re: please need help .. with swith and conditional operator?

Duoas .. really thanks alooot .. that's help me alot
Reputation Points: 10
Solved Threads: 0
Light Poster
DREAMER546 is offline Offline
27 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how do I have txt file be read by the compiler?
Next Thread in C++ Forum Timeline: quaterly sales statistics





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC