The Division Surprise
Try the maths operators and meet integer division.
1print(7 + 3)
2print(7 * 3)
3print(7 / 2)
4print(7.0 / 2)
$ swift main.swift
10
21
3
3.5
Every line, explained
print(7 + 3)- + adds two numbers together.
print(7 * 3)- * means multiply: there is no × key on a keyboard, so programmers use the asterisk.
print(7 / 2)- Surprise! This prints 3, not 3.5. Both 7 and 2 are whole numbers (Ints), and dividing an Int by an Int in Swift gives an Int: the decimal part is chopped off.
print(7.0 / 2)- Make either number a decimal and Swift switches to decimal division: 3.5. Something to remember whenever you divide!