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  ww  .ja v  a  2  s .co  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