You can use && or || in the switch statement? Visual C #

So, I made the game Scookor Rock Paper Scissor, and I made adjustments to it to include life and other things. Now I am 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 can I translate this into a switch statement?

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

It doesn't seem to work.

+4
source share
7 answers

The simple answer is no. You cannot use it.

The switch works with a single expression .

See MSDN for more details .

You can try the following: -

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

EDIT: -

As Jeppe Stig Nielsen comments 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.

. if.

, :

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

# switch :

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

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

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

. if:

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

, ?

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

, || && bool if.

@EricLippert , swtich .

+3

, , , playerscore = 3 pcscore = 2, || pcscore ?

+1

, , , 5, 10 , , 1, .

//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 .

+1

: switch , :

switch (playerscore || pcscore)

"if"

0

Suppose playerscore and pcscore are integers that have 0 or 1 as possible values

    resp = playerscore + 10 * pcscore;
    switch (resp)
    {
        case 0:
            // both are false
            break;
        case 1:
            // playerscore true
            break;
        case 10:
            // pcscore true
            break;
        case 11:
            // both are true
            break;
        default:
            // error in input data
            break;
    }
0
source

All Articles