Swift else if

Swift else if

Chain multiple conditions with else if for more than two branches.


Branch with else if conditions

Use else if for additional conditions when the first if is false.

Example

let score = 72
if score >= 90 {
  print("A")
} else if score >= 80 {
  print("B")
} else if score >= 70 {
  print("C")
} else {
  print("Below C")
}

This example evaluates multiple ranges using chained else if clauses.


REMOVE ADS


Temperature Levels

Use multiple else if branches to categorize values.

Example

let t = 0
if t <= 0 {
  print("Freezing")
} else if t < 10 {
  print("Cold")
} else if t < 20 {
  print("Cool")
} else {
  print("Warm")
}

Comments

Leave a Reply

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