please need help .. with swith and conditional operator?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 27
Reputation: DREAMER546 is an unknown quantity at this point 
Solved Threads: 0
DREAMER546's Avatar
DREAMER546 DREAMER546 is offline Offline
Light Poster

please need help .. with swith and conditional operator?

 
0
  #1
Oct 29th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #2
Oct 29th, 2007
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,873
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

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

 
0
  #3
Oct 29th, 2007
switch( x>y ) { ... } ?
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 27
Reputation: DREAMER546 is an unknown quantity at this point 
Solved Threads: 0
DREAMER546's Avatar
DREAMER546 DREAMER546 is offline Offline
Light Poster

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

 
0
  #4
Oct 29th, 2007
Duoas .. thanx alot ..

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

please any one answer me in q1
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #5
Oct 29th, 2007
Sure. Boolean is an ordinal type.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 27
Reputation: DREAMER546 is an unknown quantity at this point 
Solved Threads: 0
DREAMER546's Avatar
DREAMER546 DREAMER546 is offline Offline
Light Poster

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

 
0
  #6
Oct 29th, 2007
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 ..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #7
Oct 29th, 2007
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 27
Reputation: DREAMER546 is an unknown quantity at this point 
Solved Threads: 0
DREAMER546's Avatar
DREAMER546 DREAMER546 is offline Offline
Light Poster

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

 
0
  #8
Oct 29th, 2007
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 ..
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #9
Oct 29th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 27
Reputation: DREAMER546 is an unknown quantity at this point 
Solved Threads: 0
DREAMER546's Avatar
DREAMER546 DREAMER546 is offline Offline
Light Poster

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

 
0
  #10
Oct 29th, 2007
Duoas .. really thanks alooot .. that's help me alot
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1124 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC