Swift Basics - Switch Case

Switch case:
switch speed
{
  case 10: print("10")
  case 50: print("50")
  default: print("Other")
}

Multiple:
var c: Character = "e"
switch c
{
  case "a", "e", "i", "o", "u": print("Vowel")
 default: print("Consonant")
}

Switch within a range:
switch x
{
  case 85...100: print("A")
  case 70...84: print("B")
  default: print("Others")
}

Adding other conditionals to a case:
switch x
{
  case 85...100 where studentId == 8:
  print("Congratulations!")
  case 85...100:
  print("A")
}
Note that if you place where studentId == 8 above case 85...100, it would never be reached as the first condition to meet the criteria will be reached.

Comments

Popular posts from this blog

Sharing on social media

Navigation controller