Results 1 to 8 of 8

Thread: Switch statement with < or >

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    long island, new york
    Posts
    32

    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?

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2002
    Location
    long island, new york
    Posts
    32
    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?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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.

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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 );

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width