Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!