Swift - Switches without break

Introduction

Switches in swift don't need break statement as a case statement doesn't fall through by default.

You don't need to add a break to the end of all your cases.

To fall through a case to those below, Swift has the fallthrough keyword for this very reason:

Demo

let fallthroughSwitch = 10 
switch fallthroughSwitch { 
case 0..<20: //  w w  w. ja v a2  s  .  c  om
    print("Number is between 0 and 20") 
    fallthrough 
case 0..<30: 
    print("Number is between 0 and 30") 
default: 
    print("Number is something else") 
}

Result

Related Topic