Operators, Numbers and Strings in Swift: Arithmetic, Comparison, Ranges, Overflow and String Interpolation

Swift 55 min min read Updated: Nov 21, 2026 Beginner
Operators, Numbers and Strings in Swift: Arithmetic, Comparison, Ranges, Overflow and String Interpolation
Beginner Topic 3 of 3

Arithmetic Operators

swift print(7 + 2, 7 - 2, 7 * 2, 7 / 2, 7 % 2) print(7.0 / 2.0) var count = 10 count += 5 count *= 2 print(count)
Output

9 5 14 3 1 | 3.5 | 30

Notice that 7 / 2 is 3. When both numbers are Int, Swift gives a whole number. Use Double to get 3.5.

Comparison and Logic

swift let age = 20 print(age >= 18, age == 20, age != 30) let hasTicket = true print(age >= 18 && hasTicket, age < 18 || hasTicket, !hasTicket) let label = age >= 18 ? "adult" : "minor" // ternary operator print(label)
Output

true true true | true true false | adult

Range Operators

OperatorMeaningExample
a...bClosed range: includes both ends1...3 is 1, 2, 3
a..<bHalf open range: excludes b1..<3 is 1, 2
a...From a to the endused with arrays
swift for i in 1...3 { print("closed", i) } for i in 1..<3 { print("half-open", i) }
Output

closed 1 | closed 2 | closed 3 | half-open 1 | half-open 2

Overflow Is a Crash, Not a Surprise

In many languages a number that grows too big wraps around silently. In Swift it stops the program, which is safer. If you really want wrapping, use the special operators that start with &, or ask whether overflow happened.

swift let big = Int8.max print(big) let wrapped = big &+ 1 // wrapping add print(wrapped) let (value, overflow) = big.addingReportingOverflow(1) print(value, overflow)
Output

127 | -128 | -128 true

Decimal Numbers Are Approximate

swift let x = 0.1 + 0.2 print(x) print(x == 0.3) print(abs(x - 0.3) < 0.000001)
Output

0.30000000000000004 | false | true

Computers store most decimals in binary, so tiny differences appear. Compare decimals with a small tolerance, and use whole numbers (for example cents) for money.

Strings

swift let first = "Swift" let second = "Language" let full = first + " " + second print(full, full.count) print("Total: \(2 + 3) items") print(full.uppercased(), full.lowercased()) print(full.hasPrefix("Sw"), full.contains("gua")) let multi = """ Line one Line two """ print(multi)
Output

Swift Language 14 | Total: 5 items | SWIFT LANGUAGE swift language | true true | Line one | Line two

Strings are Unicode A Swift String handles emoji and accented letters correctly. count counts what a person sees as characters, not bytes.

Common Mistakes

  • Expecting 7 / 2 to give 3.5 with Int values
  • Comparing Doubles with ==
  • Using Double for money instead of whole cents

Practice

  1. Compute the average of three Int values as a Double
  2. Print a sentence using string interpolation
  3. Test what Int.max + 1 does by using &+ instead

Conclusion

Swift operators are familiar, but the compiler protects you from silent overflow and mixed types.

Quick Summary: Arithmetic; logic; ranges; overflow; decimals; strings.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators