Possible to use && or || in switch statement? Visual C#

14,312

Solution 1

The simple answer is No. You cant use it like that.

Switch works with single expression.

You may check MSDN for details.

You may try like this:-

  if (playerscore == pcscore)
        {
            switch (playerscore)
            {
                case 1:
                PlayerLife.Image = Properties.Resources.Five;
                break;
            }
        }

EDIT:-

As commented by Jeppe Stig Nielsen in the comments, You can switch on any expression of a suitable type. That expression may contain ||. There can be many case labels associated with each switch section in a switch block.

But personally speaking that would not be a good practice to follow. You may try to use if statement for that.

You may try like this if you want:

 switch (playerscore == 1 || pcscore == 1)

Solution 2

In C#, a switch statement resolves a single expression and compares that value with a list of possible cases:

switch(someExpression)
{
   case x: // This runs if someExpression == x
      break;
   case y: // This runs if someExpression == y
      break;
}

Now, you could switch on the expression (playerscore == 1 || pcscore == 1) like so:

switch(playerscore == 1 || pcscore == 1) // This expression is either true or false
{
   case true: // Runs if playerscore is 1 or pcscore is 1
      break;
   case false: // runs if neither playscore or pcscore are 1
      break;
}

However, the above is rather unreadable and silly. You'd be best off with the if statement:

if(playerscore == 1 || pcscore == 1)
{
   // Runs if playerscore is 1 or pcscore is 1
}
else
{
   // runs if neither playscore or pcscore are 1
}

Solution 3

You could write it like this but why would you want to?

  switch (playerscore == 1 || pcscore == 1)
        {
            case true:
                PlayerLife.Image = Properties.Resources.Five;
                break;
            default:
                break;
        }

As Jeppe points out in the comment below, when you use || or && you end up with a bool and an if statement should be used.

Here is a great answer by @EricLippert on what can be used as the expression in a swtich statement.

Solution 4

What you are trying to do doesn't make sense, if playerscore = 3 and pcscore = 2 then what would playerscore || pcscore be equal to?

Solution 5

If you have a whole bunch of variables, say not just two, but 5, 10, or even an unknown number, then what you can do is put all of the values that you want to compare to 1 into a collection and then act on that collection as a whole.

//this could just be a list/array accepted as a paramter, 
//can include other variables, or whatever
var scores = new []{playerscore, pcscore};

if(scores.Any(score => score == 1))
    PlayerLife.Image = Properties.Resources.Five;

switch isn't really an appropriate tool for manipulating collections like this.

Share:
14,312
user2911044
Author by

user2911044

Updated on June 27, 2022

Comments

  • user2911044
    user2911044 almost 2 years

    So I was making a Rock Paper Scissor game and I've sort of made adjustments to it to include life and other things. Now I got stuck with the switch statement. My if statement works fine:

    private void Result_TextChanged(object sender, EventArgs e)
    {
         if (playerscore == 1 || pcscore == 1)
         {
              PlayerLife.Image = Properties.Resources.Five;
         }
    }
    

    I was wondering how I could translate this to the switch statement?

    private void Result_TextChanged(object sender, EventArgs e)
    {              
        switch (playerscore || pcscore)
        {
             case 1:
                 PlayerLife.Image = Properties.Resources.Five;
                 break;
        }
    }
    

    Doesn't seem to work.

  • user2911044
    user2911044 over 10 years
    Oh okay. So if I wanted to make it a switch statement I have to separate the playerscore and pcscore?
  • DaveD
    DaveD over 10 years
    I think he meant "if ((playerscore == 1) || (pcscore == 1))"
  • Tim S.
    Tim S. over 10 years
    playerscore || pcscore doesn't resolve at all in C#: they're both numbers, and unlike other languages, not everything has a truthiness.
  • Rahul Tripathi
    Rahul Tripathi over 10 years
    @user2911044:- I think it would be better if you can use if statement for that. As a suggestion dont violate the rules of C#.
  • user2911044
    user2911044 over 10 years
    Yeah I just realised my mistake. Hah looks like I'll be able to use switch statement after all :)
  • Harrison
    Harrison over 10 years
    This is more of a comment than an answer, isn't it?
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 10 years
    Downvote. You can switch on any expression of a suitable type. That expression may contain ||. There can be many case labels associated with each switch section in a switch block.
  • Rahul Tripathi
    Rahul Tripathi over 10 years
    @JeppeStigNielsen:- Yes I agree Sir but I dont think that would be a great practice to follow! Do correct me if I am wrong:)
  • Servy
    Servy over 10 years
    @Harrison Saying there is no answer is an answer.
  • Mike Christensen
    Mike Christensen over 10 years
    @TimS. - Yea, I didn't read the question very carefully at all!
  • Tim S.
    Tim S. over 10 years
    I wrote this if/switch combo too, but then realized it's supposed to be playerscore == 1 || pcscore == 1, not playerscore == 1 && pcscore == 1, so that isn't right. Edit: Oh, but the title asked about both, so that'd work for the && case only.
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 10 years
    Well, regarding the situation the asker shows, I can't see a useful way to use switch for that. But in another situation you could use switch (playerScore > pcScore || isGameOver ? pcScore : playerScore) { ... } or whatever.
  • Alexei Levenkov
    Alexei Levenkov over 10 years
    +1. I think that "single variable value" is confusing, and "single value" is better (like playerscore | pcscore would be fine, but 2 variables)
  • Mike Christensen
    Mike Christensen over 10 years
    If you change Switch works with single value. to Switch works with single expression. I'll give you an upvote.
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 10 years
    While this works, why switch on an expression of type bool? It is much clearer to use if or ifelse for that!
  • Harrison
    Harrison over 10 years
    @JeppeStigNielsen I agree completely, that's why I commented why would you want to in the answer.
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 10 years
    I speculate that @user2911044 would like both case 3 and case 2 to run in that case? But in what order? And if the values were equal, should the relevant switch section run twice? Anyway, switch does not work like that.
  • Mike Christensen
    Mike Christensen over 10 years
    I think instead of data you meant scores
  • Servy
    Servy over 10 years
    @MikeChristensen Thanks, fixed.