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
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
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
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
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