|
-
Nov 18th, 2003, 08:46 AM
#1
Thread Starter
Member
Switch statement with < or >
i want to make a switch statementt that uses < or >. it is for a simple program that takes a grade out of 100 and assigns a letter grade. how can i do this? do i need to stick to a whole bunch of if statements, or can this be done?
-
Nov 19th, 2003, 10:50 PM
#2
Hyperactive Member
While you could try using fall-through or goto's, it'll still be a lengthy switch.
Why not KISS with a pure (?) conditional operator?
Code:
int score = 80;
string Ltr_Grade = score > 92 ? "A":
score > 80 ? "B":
score > 70 ? "C":
score > 62 ? "D": "F";
Console.WriteLine( "Your letter grade is: " + Ltr_Grade );
HTH.
-
Nov 20th, 2003, 09:27 AM
#3
Thread Starter
Member
Originally posted by Mongo
While you could try using fall-through or goto's, it'll still be a lengthy switch.
Why not KISS with a pure (?) conditional operator?
Code:
int score = 80;
string Ltr_Grade = score > 92 ? "A":
score > 80 ? "B":
score > 70 ? "C":
score > 62 ? "D": "F";
Console.WriteLine( "Your letter grade is: " + Ltr_Grade );
HTH.
hmmm... the only c# i know is from a high school course i'm taking. i have never seen this operator before. what does it do/how does it work?
-
Nov 20th, 2003, 10:29 AM
#4
cond ? expr1 : expr2
If cond is true, the result is expr1. Else it's expr2.
However nesting them like Mongo did makes for terribly unreadable code.
You can only have constants in switch cases. You should use if...else if...else chains for places where you need expressions.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 25th, 2003, 11:37 AM
#5
I like Mongo's code even though it's tough on the brain. I've not seen that particular way of using of ?: before.
It's a bugger to read though like Cornedbee said.
Would that code be faster / slower than the equivalent if() statements?
I don't live here any more.
-
Nov 25th, 2003, 11:42 AM
#6
it looks a bit more obvious like this...
Code:
int score = 80;
string Ltr_Grade =
score > 92 ? "A":
score > 80 ? "B":
score > 70 ? "C":
score > 62 ? "D":
/*default*/ "F";
Console.WriteLine( "Your letter grade is: " + Ltr_Grade );
...but not much
-
Nov 25th, 2003, 11:44 AM
#7
Or maybe...
Code:
int score = 80;
string Ltr_Grade = (score > 92 ? "A": (score > 80 ? "B": (score > 70 ? "C": (score > 62 ? "D": /*default*/ "F"))));
Console.WriteLine( "Your letter grade is: " + Ltr_Grade );
-
Nov 25th, 2003, 12:20 PM
#8
Whether if or ? is faster depends solely on the compiler.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|