MasterOnce
HomeBlogHow-To
3 min readJan 19, 2026
Swift

How to Write If-Else Logic

Branch behavior with clear, readable conditions.

Steps

  1. Paste the code into a Swift file or playground.
  2. Run it once to verify the output.
  3. Adjust the inputs to match your use case.

Copy and paste

import Foundation

let score = 82

if score >= 90 {
    print("A")
} else if score >= 80 {
    print("B")
} else {
    print("Keep going")
}

Notes

  • Keep functions small and focused for reuse.
  • Prefer safe APIs like optionals and guards.