Swift - Matching Characters in switch case statement

Introduction

You can use the Switch statement to match characters, as the following code demonstrates:

Demo

var grade: Character
grade = "A"// w  w w.  ja v a2  s  .  c  o  m
switch grade {
      case "A", "B", "C", "D":
          print("Passed")
      case "F":
          print("Failed")
      default:
          print("Undefined")
}

Result

The first case checks whether grade contains the character "A," "B," "C," or "D."

The second case tries to match the character "F." If all fails, the default case is matched.

Related Topic