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

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.

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)

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
}

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.

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

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.

This makes no sense: in a switch statement you always want to compare with a specific type, rather than to a boolean value as follows:
switch (playerscore || pcscore)
in your case use the 'if'-statement

Suppose that playerscore and pcscore are integer who has 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;
}

Related

Is it possible to check for null values in switch case statement in C#

I have an object with the property Value. Value is a nullable number. I have some scenarios for the value of value. Usually, for null cases, I use the default case but this time, it is not correct logically. I want to do "X" in case of 100, "Y" in case of no value (the value is null), otherwise, I want to do "Z".
switch (p.Value)
{
case 100:
// DO X
break;
default:
// Do Z
break;
}
I tried writing case is null but it doesn't compile: Invalid expression term 'is' (CS1525). Is it possible or should I use if statements instead?
You can check for null in a switch case, just like any other value, but you can't use is, is is used in casting
switch (p.Value)
{
case 100:
// DO X
case null:
// DO Y
break;
default:
// Do Z
break;
}
Anyway a case where the item is null is only 1 case, therefore you can just check it with an if statement that is warpped around the switch
In your case
if (p.Value == null){
//DO Y;
}else{
switch (p.Value)
{
case 100:
// DO X
break;
default:
// Do Z
break;
}
}
The first option is much cleaner, and you should choose it
Just tryed it
void Main()
{
int? p=null;
switch (p)
{
case null:
break;
case 100:
// DO X
break;
default:
// Do Z
break;
}
}
everything works fine

Why does the compiler add statements to the switch?

I have the following reasonably simple switch statement.
// earlier
string fullPath = GetFullPath();
string type = GetEntityType();
switch (type.ToLower()) {
case "tables":
tables.Add(fullPath);
break;
case "views":
views.Add(fullPath);
break;
case "functions":
functions.Add(fullPath);
break;
case "storedprocs":
storedprocs.Add(fullPath);
break;
case "data":
data.Add(fullPath);
break;
case "layouts":
layouts.Add(fullPath);
break;
case "scripts":
scripts.Add(fullPath);
break;
default:
Console.WriteLine($"What is this: {type}");
break;
}
When I decompile the resulting binary using Reflector, the switch(string) has been changed to ComputeStringHash and then inside each case statement it checks the value via the if statement. Sounds like it's doing double the work.
string s = str2.ToLower();
switch (<PrivateImplementationDetails>.ComputeStringHash(s))
{
case 0x20890fc4:
if (s == "tables")
{
break;
}
goto Label_0218;
case 0x454a414e:
if (s == "functions")
{
goto Label_01DE;
}
goto Label_0218;
case 0x4facf6d1:
if (s == "views")
{
goto Label_01D3;
}
goto Label_0218;
case 0xcdfe2cb3:
if (s == "storedprocs")
{
goto Label_01E9;
}
goto Label_0218;
case 0xd872e2a5:
if (s == "data")
{
goto Label_01F4;
}
goto Label_0218;
case 0x9b4a129b:
if (s == "scripts")
{
goto Label_020C;
}
goto Label_0218;
case 0xba971064:
if (s == "layouts")
{
goto Label_0200;
}
goto Label_0218;
default:
goto Label_0218;
}
first.Add(fullPath);
continue;
Label_01D3:
second.Add(fullPath);
continue;
Label_01DE:
list3.Add(fullPath);
continue;
Label_01E9:
list4.Add(fullPath);
continue;
Label_01F4:
list5.Add(fullPath);
continue;
Label_0200:
list6.Add(fullPath);
continue;
Label_020C:
list7.Add(fullPath);
continue;
Label_0218:
Console.WriteLine("What is this: " + str2);
}
This is a very smart optimization, which lets the switch do its job in time that is almost independent of the number of strings in the case block of the statement.
This optimization is based on the observation that hash codes of identical strings must be the same. Rather than checking strings for equality one-by-one, the compiler computes a hash of the target string once, and performs a table-based lookup in O(1). This gets the compiler to the desired case, at which point the compiler needs to check that the strings are actually equal.
Note that there would be some rare situations when multiple look-up strings would have the same hash code. In such situations the generated case statement would contain multiple if to decide among the strings with equal hash codes.
Overall, this behavior mimics the behavior of hash-based dictionaries: hash code determines the case (an equivalent of a hash bucket) and the series of ifs inside determines if there is a match. This results in a better performance, because it lets the compiler skip the unnecessary checks.

How to use && operator in switch statement based on a combined return a value?

How can I use && operator in switch case?
This is what i want to do:
private int retValue()
{
string x, y;
switch (x && y)
{
case "abc" && "1":
return 10;
break;
case "xyz" && "2":
return 20;
break;
}
}
My problem is that "abc" and "1" are both of type string and the compiler gives me message that:
"operator && cannot be applied to string"
There is no such operator in switch statements. The switch statement operates on a single variable which is a value type or a string. See:
http://www.dotnetperls.com/switch
http://msdn.microsoft.com/en-us/library/06tc147t.aspx
The real problem in your example is that both in the switch expression, and in case labels, you are applying && to strings. You cannot apply && to strings, it only works on booleans (unless you overload it and define a new function that does work on strings).
What you are trying to accomplish is probably to simultaneously check the values of two different variables with one switch. This is impossible; switch only checks one variable at a time. The solution is to use if statements or a specialized CheckStrings(string s1, string s2) method (which may or may not use if statements).
In a comment you have expressed concerns with length. Observe:
private int retValue(string x, string y)
{
if (x == "abc" && y == "1") return 10;
if (x == "xyz" && y == "2") return 20;
throw new Exception("No return value defined for these two strings.")
}
Shorter, even if you discount the gains from skipping redundant break; statements and putting returns on the same line.
Despite there is an accepted answer already...
To achieve logical AND in switch, it has to be done like this:
switch(x + y)
{
case "abc1":
return 10;
break;
case "xyz2":
return 20;
break;
}
Which works.
For logical OR see zey answer.
You mean like that ?
switch (value)
{
case "abc":
case "1":
return 10;
case "xyz":
case "2":
return 20;
}
If you are using C# 8 and above below code snippet will yield the desired result. This is using pattern matching with expression future.
string x = "abc", y = "2";
var result = (x, y) switch
{
("abc","1") => 10,
("xyz","2") => 20,
(_,_) => 0
};
Console.WriteLine($"Arguments : {x}, {y}, result : {result}");
switch statement can only be applied to integer values or constant expressions. If you want to check your conditions on string type variable, then you should use if-else-if structure.

If Condition inside switch case [duplicate]

This question already has answers here:
Control cannot fall through from one case label
(8 answers)
Closed 9 years ago.
I am trying to convert an if statement to switch cases (for readability)
1) I've read switch statements are aweful in general - Is that true?
https://stackoverflow.com/questions/6097513/switch-statement-inside-a-switch-statement-c
2) The statement goes like this:
switch (Show)
{
case Display.Expense:
if (expected.EXPENSE != true)
break;
case Display.NonExpense:
if (expected.EXPENSE == true)
break;
case Display.All:
//Code
break;
}
Error is:
Control cannot fall through from one case label ('case 1:') to another
This is the original if statement:
if ((Show == Display.All) || (expected.EXPENSE == true && Show == Display.Expense) || (expected.EXPENSE == false && Show == Display.NonExpense))
{
//Code
}
First off, I notice that you forgot to ask a question in your second point. So I'm going to ask some questions for you addressing your second point:
What is the meaning of the "can't fall through" error?
Unlike C and C++, C# does not allow accidental fall-through from one switch section to another. Every switch section must have an "unreachable end point"; it should end with a break, goto, return, throw or (rarely) infinite loop.
This prevents the common bug of forgetting to put in the break and "falling through" accidentally.
You've written your code as though fall-through is legal; my guess is that you're a C programmer.
How can I force fall-through in C#?
Like this:
switch (Show)
{
case Display.Expense:
if (expected.EXPENSE != true)
break;
else
goto case Display.All;
case Display.NonExpense:
if (expected.EXPENSE == true)
break;
else
goto case Display.All;
case Display.All:
//Code
break;
}
Now the reachability analyzer can determine that no matter which branch of the "if" is taken, the switch section endpoint is unreachable.
Is this good style?
No. Your original code was a lot more readable.
I've read switch statements are aweful in general - Is that true?
Opinions vary. Switch statements are very useful when there is a small number of very "crisp" alternatives whose behaviours do not interact in complex ways. Some people will tell you that switched logic should instead be handled by virtual methods or visitor patterns, but that can be abused as well.
Should I use a switch in this particular case?
I wouldn't.
How would you improve my code?
if ((Show == Display.All) ||
(expected.EXPENSE == true && Show == Display.Expense) ||
(expected.EXPENSE == false && Show == Display.NonExpense))
{
//Code
}
First off, don't name things IN ALL CAPS in C#.
Second, don't compare Booleans to true and false. They're already Booleans! If you want to know the truth of statement X you would not say in English "is it true that X is true?" You would say "Is X true?"
I would likely write:
if (Show == Display.All ||
Show == Display.Expense && expected.Expense ||
Show == Display.NonExpense && !expected.Expense)
{
//Code
}
Or, even better, I would abstract the test away into a method of its own:
if (canDisplayExpenses())
{
//Code
}
Or abstract the whole thing away:
DisplayExpenses();
The compiler will not understand what you mean here.
switch (Show)
{
case Display.Expense:
if (expected.EXPENSE != true)
break;
// missing break here
case Display.NonExpense:
The compiler will not connect the dots and understand that the break; statement inside your if statement is linked to the switch statement. Instead it will try to link it to a loop, since break; statements on their own can only be used with loops, to break out of it.
That means that your case block is missing its break statement to complete it, and thus the compiler complains.
Instead of trying to wring the necessary code out of a switch statement, I would instead break up your original if statement.
This is yours:
if ((Show == Display.All) || (expected.EXPENSE == true && Show == Display.Expense) || (expected.EXPENSE == false && Show == Display.NonExpense))
{
//Code
}
This is how I would write it:
bool doDisplayExpected =
(Show == Display.All)
|| (Show == Display.Expense && expected.EXPENSE)
|| (Show == Display.NonExpense && !expected.EXPENSE);
if (doDisplayExpected)
{
// code
}
You don't have to pack everything on one line.
Also, I would try to name properties so that they're easier to read, I would rename the EXPENSE property to IsExpense so that the above code would read like this:
bool doDisplayExpected =
(Show == Display.All)
|| (Show == Display.Expense && expected.IsExpense)
|| (Show == Display.NonExpense && !expected.IsExpense);
if (doDisplayExpected)
{
// code
}
Then, ideally, I would refactor out the sub-expressions to methods:
bool doDisplayExpected =
ShowAll()
|| ShowExpense(expected)
|| ShowNonExpense(expected);
if (doDisplayExpected)
{
// code
}
public bool ShowAll()
{
return Show == Display.All;
}
public bool ShowExpense(Expected expected)
{
return Show == Display.Expense && expected.EXPENSE;
}
public bool ShowNonExpense(Expected expected)
{
return Show == Display.NonExpense && !expected.EXPENSE;
}
Then you can put the expression back into the if-statement:
if (ShowAll() || ShowExpense(expected) || ShowNonExpense(expected))
{
// code
}
This should be easier to read, and change later on.
Use if statements and extract complex conditions into methods, e.g
if (ShowAll() || ShowExpense())
{
}
Remember about OOP and polymorphism every time you write such 'switch', adding another
case to that code will be a nightmare
see this and similar (C++) instructions about converting switches
P.S if you are interested in making your code clean and readable, consider reading Smalltalk Best Practice Patterns by Kent Beck and/or Clean Code by Uncle Bob
I really enjoyed both of them, highly recommend.
If you want readability, just throw away your syntax trash:
if (Show == Display.All || expected.EXPENSE && Show == Display.Expense || !expected.EXPENSE && Show == Display.NonExpense)
{
//Code
}
Provide the else part for each of them so it will not throw error, however as others say, you actually don't need switch in this case.
switch (Show)
{
case Display.Expense:
if (expected.EXPENSE != true)
// do what you want
break;
else
// do what you want
break;
case Display.NonExpense:
if (expected.EXPENSE == true)
// do what you want
break;
else
// do what you want
break;
case Display.All:
//Code
break;
}
The reason why you get this error is that you are not defining break statements.
You defined the break conditionally.
switch (Show)
{
case Display.Expense:
if (expected.EXPENSE != true)
break;
// Note that the break above is in scope of you if statement, and will
// result in a compiler error
case Display.NonExpense:
...
}
Either make sure every case statement has its own break or group the case statements as follows.
switch (Show)
{
case Display.Expense:
case Display.All:
// do stuff
// Expense and All have the same behavior
}
Refactor out the if statements so you can express it like so:
if (isDisplayAll() || isExpense(expected) || isNonExpense(expected))
{
// Code
}
The extracted logic:
private bool isDisplayAll()
{
return (Show == Display.All);
}
private bool IsExpense(Expected expected)
{
return expected.EXPENSE && (Show == Display.Expense);
}
private bool IsNonExpense(Expected expected)
{
return !expected.EXPENSE && (Show == Display.NonExpense);
}
Agree with Dennis, you don't want a switch case for this problem.
Although probably less readable, you can also use shorter:
if (Show == Display.All || (expected.EXPENSE == (Show == Display.Expense)))
{
//Code
}

Multiple Switch statements with multiple cases?

I'm trying to write a switch statement that would have a similar effect to what I show in the code below, but (obviously), it's not working. It will be impossible to have something selected in both combo boxes at once. Any thoughts on how to accomplish this? I'm updating an older program, and I'm trying not to re-write a large chunk of the code.
switch ((cboMAIN.SelectedIndex) || (cboMAINalternate.SelectedIndex))
{
case 0:
OutputString1 = "A";
break;
case 1:
OutputString1 = "C";
break;
case 2:
OutputString1 = "E";
break;
case 3:
OutputString1 = "F";
break;
case 4:
OutputString1 = "I";
break;
case 5:
OutputString1 = "J";
break;
case 6:
OutputString1 = "K";
break;
}
Try using
switch ((cboMAIN.SelectedIndex > -1) ? cboMAIN.SelectedIndex : cboMAINalternate.SelectedIndex)
{
No, that's not possible (at least not the way you're doing it).
The question would be, what are you trying to achieve? If you'd have some kind of toggle to switch between both indexes, you could do something like that:
bool useAlternative = ...;
switch(useAlternative ? cboMAINalternate.SelectedIndex : cboMAIN.Selectedindex) {
// ...
}
There is always a selected item in a select element, so you have to check for the one that you consider to be no selecton, for example the first item:
var item = cboMAIN.SelectedIndex;
if (item = 0) item = cboMAINalternate.SelectedIndex;
switch (item) {
...
Probably the concisest way:
switch (Math.Max(cboMAIN.SelectedIndex, cboMAINalternate.SelectedIndex))
{
// ...
}
According to MSDN, SelectedIndex will return -1 if the selection is empty on a ComboBox.
The following code should choose a ComboBox with a selection and go through your switch statement, or fail if neither has a selection:
ComboBox comboBoxToUse;
if (cboMAIN.SelectedIndex > -1)
{
comboBoxToUse = cboMAIN;
}
else if (cboMAINalternate.SelectedIndex > -1)
{
comboBoxToUse = cboMAINalternate;
}
else
{
throw new InvalidOperationException("Neither combo box contains a selection.");
}
switch (comboBoxToUse.SelectedIndex)
{
...
}
You may also want to throw an exception if both ComboBoxes have a selection.

Categories

Resources