1

So I have a task from a worksheet at work:

I have to change these Ifs to a switch, but how do i do it? Heres the Ifs:

if (i<0 || i>15)  Console.WriteLine ("A");
else if (i%5<2 && i/3>3)  Console.WriteLine ("B");
else if (3<i && i<10)  Console.WriteLine ("C");
else if (i&2==2)  Console.WriteLine ("D");
else  Console.WriteLine ("E");

and here is the switch i made, but that one is bad, but i dont know how to make a good one out of it, i hope you can help me with this.

switch (i)
    case (i<0):
    case (i>15):
        Console.WriteLine ("A“)
        Break;
    Case (i%5<2 && i/3>3) : 
        Console.WriteLine ("B“)
        Break;
    case (3<i && i<10) :
        Console.WriteLine ("C");
        Break;
    Case (i&2==2)  :
         Console.WriteLine ("D");
        Break;
    Default
         Console.WriteLine ("E");
        Break;

It doesnt have to run in a programm, it's just a task from a worksheet

8
  • what is the reason you need to use a switch? Commented Nov 21, 2014 at 9:19
  • Its a task from a worksheet at work Commented Nov 21, 2014 at 9:20
  • 2
    FWIW I think the downvotes are a bit harsh here. Given the problem set, its a fair question IMHO Commented Nov 21, 2014 at 9:29
  • 4
    @Mike: there are no stupid questions. Agree that the down votes are a bit harsh. Commented Nov 21, 2014 at 9:32
  • 1
    @EmilLundin: Have figured out what's going on. Its not really a question about the limitations of switch so much as getting the @Mike to understand the logic of the if statement itself (see my answer). Commented Nov 21, 2014 at 9:38

4 Answers 4

5

I haven't figured out all the rules completely, but I think what you're after is something like this:

The question wants you to realise that you've got a limited number of results, since the first rule is (i<0 || i>15). This can be the default for a switch statement.

Then you need to work out what would happen for all integers 0-15 and write them into the rest of the switch.

So you're after something along the lines of (although this doesn't have your logic - you can figure that out so you understand what's going on):

switch (i) 
{

    case 0:
    case 2:
    case 5:
        Console.Write("Something")
        break;
    case 1:
    case 7:
        Console.Write("Something Else")
        break;

    default
        Console.WriteLine ("A“)
        Break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think thats what this is supposed to be, thanks! I hope i can make something out of that :)
3

I think it's:

switch (i)
{
  case 4:
  case 5:
  case 6:
  case 7:
  case 8:
  case 9:
      Console.WriteLine("C");
      break;
  case 0:
  case 1:
  case 3:
  case 10:
  case 11:
  case 12:
  case 13:
  case 14:
     Console.WriteLine("E");
     break;
  case 2:
     Console.WriteLine("D");
     break;
  case 15:
     Console.WriteLine("B");
     break;
  default:
     Console.WriteLine("A");
     break;
}

4 Comments

@BOAndrew: Nicely figured out, although I was trying not to give the answer on a plate!
I didnt really get the task, but thanks to your answer @Jon Egerton, i know that im stupid :D, Im gonna figure that out myself, but i may check back and look at this :) Thanks!
OK... maybe that was not so smart move from me to post the solution instead of how I would try to find it. But Jon, at least we don't have redundant answers :)
I solved it myself now, thanks to your help Jon and Andrew :), i just didnt get that there is a logic behind these, i feel ashamed :(. I can just mark one answer as correct tho
2

Use a separate variable, in which you have to embed the segmentation logic. Here is a sample:

//using enum
public enum MySwitchableVariable {
    NotDefined, //optional
    LessThanZero,
    MoreThanFifteen,
    RestOfFiveLessThanTwoAndFactorMoreThanThree, //its too complex to describe
    BetweenThreeAndTen,
    RestOfTwoIsTwo,
}

Then you have to make a function which takes an integer and spit out a case of this enum:

public MySwitchableVariable calculate(int i) {
    var result = MySwitchableVariable.NotDefined;
    // your implementation here
    return result;
}

finally you may switch that annoying variable like this:

var Variable = calculate(i);
switch(Variable) {
    case MySwitchableVariable.LessThanZero:
    // you know the rest

is that what you (or your instructor) want?

2 Comments

Im not sure, i dont think thats what he wants, I think its more like what Jon Egerton said, maybe that could help me, but thanks!
This is an interesting approach, although what you end up with in the calculate method is the original if statements, so doesn't really help in this particular instance.
1

Simply you can't do this. Using the switch statement entails that the variable in switch can take a number of discrete constant values.

From the documentation:

The switch statement is a control statement that selects a switch section to execute from a list of candidates.

Furthermore,

Each case label specifies a constant value. The switch statement transfers control to the switch section whose case label matches the value of the switch expression (caseSwitch in the example). If no case label contains a matching value, control is transferred to the default section, if there is one. If there is no default section, no action is taken and control is transferred outside the switch statement. In the previous example, the statements in the first switch section are executed because case 1 matches the value of caseSwitch.

3 Comments

I dont see why i get this as a task then :/
@Mike have this been assigned to you as a homework or have you been asked to do this in a job? Could you please give use more information (despite the fact that I think there aren't any more information) of what you have been asked? Thanks
yea, its just a task someone gave me. Its a worksheet with different Tasks on it to help me get into programming and C#. This one task just says, change these if statements to a switch statement, not related to any programm or anything, these are the only information i got tho.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.