Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda