Swift else

Swift else

Use else to handle the false branch of a condition.


Handle the false branch with else

Use else to run an alternate block when the condition is false.

Example

let hasAccess = false
if hasAccess {
  print("Welcome")
} else {
  print("Denied")
}


Age Gate

Use else to handle when a user does not meet a requirement.

Example

let age = 16
if age >= 18 {
  print("Access granted")
} else {
  print("Access denied")
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *