Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda