Arithmetic Operators
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
true true true | true true false | adult
Range Operators
| Operator | Meaning | Example |
|---|---|---|
| a...b | Closed range: includes both ends | 1...3 is 1, 2, 3 |
a..<b | Half open range: excludes b | 1..<3 is 1, 2 |
| a... | From a to the end | used with arrays |
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.
127 | -128 | -128 true
Decimal Numbers Are Approximate
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 Language 14 | Total: 5 items | SWIFT LANGUAGE swift language | true true | Line one | Line two
count counts what a person sees as characters, not bytes.
Common Mistakes
- Expecting
7 / 2to give 3.5 with Int values - Comparing Doubles with
== - Using Double for money instead of whole cents
Practice
- Compute the average of three Int values as a Double
- Print a sentence using string interpolation
- Test what
Int.max + 1does by using&+instead
Conclusion
Swift operators are familiar, but the compiler protects you from silent overflow and mixed types.

