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

239
Views
Multiple switch statement with conditional inside

Is there a cleaner way of writing the conditional for case "1", "4", "7"? For this case I want to write to the string "b" only if the string value is not "4".

var b = ""
var c = ""

let s = "4"

switch s {
case "0", "6", "8":
    c += "|_|"
case "1", "4", "7":
    if s != "4" { b += "  |" }
    c += "  |"
case "2":
    c += "|_ "
case "3", "5", "9":
    c += " _|"
default:
    c += ""
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is a place you could use the fallthrough statement:

switch s {
case "0", "6", "8":
    c += "|_|"
case "1", "7":
    b += "  |"
    fallthrough
case "4":
    c += "  |"
case "2":
    c += "|_ "
case "3", "5", "9":
    c += " _|"
default:
    break
}

Since you want "1" and "7" to execute both statements, give them their own case and then follow it with fallthough which allows it to fall though to the next case. Then "1", "4", and "7" will all execute the code in the case "4": case.

over 4 years ago · Santiago Trujillo Report

0

I think the whole idea of switch statement is that you don’t use ifs inside. You should probably just create a separate case for “4”. The amount of space saved by putting this conditional isn’t worth obscuring the code IMO.

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!