Labs ICT
โญ Pro Login

Numbers and Operators

Math, comparisons, and type conversions.

Numbers and Operators

Swift handles numbers with ease. You have Int for whole numbers, Double for decimal numbers, and Float for smaller decimals. Most of the time, you'll use Int and Double.

Basic math operations work as you'd expect:

let a = 10
let b = 3
let sum = a + b      // 13
let difference = a - b // 7
let product = a * b   // 30
let quotient = a / b  // 3 (integer division)

Need to convert between types? Use initializer syntax like Double(someInt). This is important because Swift won't automatically convert types for you.

Comparison operators help you make decisions: == for equality, != for inequality, < and > for comparisons. Compound assignment operators like += and -= let you update values concisely.

Want random numbers? Use Int.random(in: 1...10) to get a random integer between 1 and 10 inclusive.

Try it Yourself ->

๐Ÿงช Quick Quiz

What is the result of 3 + 4 in Swift?