954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

please need help .. with swith and conditional operator?

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 :)

DREAMER546
Light Poster
27 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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:

cout << (
  (grade == 'A') ? "excellent" : (
    (grade == 'B') ? "very good" : (
      (grade == 'c') ? "good" : "not so good at all" )))
  << endl;

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

switch( x>y ) { ... } ?

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

Duoas .. thanx alot .. :)

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

please any one answer me in q1

DREAMER546
Light Poster
27 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Sure. Boolean is an ordinal type.

switch (x < y) {
  case true:
    // hmm, x < y
    break;
  case false:
    // wait, x >= y
  }

[EDIT] man, too slow again...

No probDREAMER546 :-)

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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

DREAMER546
Light Poster
27 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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!

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

DREAMER546
Light Poster
27 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

This is an expression: x > y . It evaluates to an integral (aka ordinal) value (specifically, abool), which is what the switch statement takes. So when you say
switch (something) {
as long assomething 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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Duoas .. really thanks alooot :) .. that's help me alot

DREAMER546
Light Poster
27 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You