'Two exclusive conditions along with one or condition for two values in switch statement
Is there a way to do
switch (expr) {
case space or tab:
print 'one of em + '
case space:
print 'space'
case tab:
print 'tab'
}
| Input | Output |
|---|---|
| tab | one of em + tab |
| space | one of em + space |
If not, what are the alternatives? I know I could use if else but since I'm only using one expression as input, switch seems more appropriate
Solution 1:[1]
You cannot do this with a switch.
But with if it's really simple and readable:
if (expr == space || expr == tab)
print 'one of em + '
if (expr == space)
print 'space'
else if (expr == tab)
print 'tab'
or better:
if (expr == space || expr == tab)
{
print 'one of em + '
if (expr == space)
print 'space'
else if (expr == tab)
print 'tab'
}
or even better:
if (expr == space || expr == tab)
{
print 'one of em + '
if (expr == space)
print 'space'
else // if (expr == tab) is not needed because
print 'tab' // tab is the only possible outcome here.
}
All code above is pseudo code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
