Mike
Mike

Reputation: 57

C# Change If to switch, how to handle "and"

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

Upvotes: 1

Views: 140

Answers (4)

Jon Egerton
Jon Egerton

Reputation: 41559

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;
}

Upvotes: 5

Andrei Bucurei
Andrei Bucurei

Reputation: 2438

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;
}

Upvotes: 3

nilbot
nilbot

Reputation: 345

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?

Upvotes: 2

Christos
Christos

Reputation: 53958

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.

Upvotes: 1

Related Questions