Swift Range and Operators: Exploring the Basics

Swift Range and Operators: Exploring the Basics

ยท

5 min read

In Swift, operators are symbols or keywords used to perform specific operations on values, while range is a special type used to represent a sequence of values. Let's dive into each in more detail.

Operators

Swift provides a wide range of operators, including arithmetic, comparison, assignment, logical, and bitwise operators, among others. Here are some examples:

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations on numerical values.

Swift provides the standard arithmetic operators, including addition (+), subtraction (-), multiplication (*), and division (/). Swift also supports the modulo operator (%), which returns the remainder of a division operation.

// Initializing the variables a and b
let a = 5
let b = 2

// Addition (+)
let sum = a + b // 7

// Subtration (-)
let difference = a - b // 3

// Multiplication (*)
let product = a * b // 10

// Division (/)
let quotient = a / b // 2

// Modulo operator (%)
let remainder = a % b // 1

Comparison Operators

Comparison operators are used to compare two values and return a Boolean value indicating whether the comparison is true or false.

Swift provides a range of comparison operators, including greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), equal to (==), and not equal to (!=). These operators are used to compare two values and return a Boolean result (true or false).

// Initializing the variables a and b
let x = 5
let y = 10

// Equal to (==)
let isEqual = x == y // false

// Not Equal to (!=)
let isNotEqual = x != y // true

// Greater than (>)
let isGreater = x > y // false

// Lesser than (<)
let isLess = x < y // true

// Greater than Equal to (>=)
let isGreaterOrEqual = x >= y // false

// Lesser than Equal to (<=)
let isLessOrEqual = x <= y // true

Assignment Operators

Assignment operators are used to assign a value to a variable or constant.

// Initializing the variable c
var c = 5

// c + 2 = 5 + 2 = 7
c += 2 // c is now 7

// c - 3 = 7 - 3 = 4
c -= 3 // c is now 4

// c * 2 = 4 * 2 = 8
c *= 2 // c is now 8

// c / 2 = 8 / 2 = 4
c /= 2 // c is now 4

// c % 3 = 4 % 3 = 1
c %= 3 // c is now 1

Logical Operators

Swift provides logical operators, including AND (&&), OR (||), and NOT (!). These operators are used to evaluate logical expressions and return a Boolean result.

// Initializing the variables p and q
let p = true
let q = false

// and (&&) = both must be same to get value as true
let and = p && q // false

// or (||) = any one must be true to get value as true
let or = p || q // true

// not (!) = oposities the value of the varible
let not = !p // false

Bitwise Operators

Swift provides bitwise operators, including AND (&), OR (|), XOR (^), NOT (~), and left shift (<<) and right shift (>>). These operators are used to manipulate the individual bits of an integer value.

// Initializing the variables d and e
let d = 0b1010
let e = 0b1100

// AND (&)
let and = d & e // 0b1000

// OR (|)
let or = d | e // 0b1110

// XOR (^)
let xor = d ^ e // 0b0110

// Left Shift (<<)
let shiftLeft = d << 1 // 0b10100

// Right Shift (>>)
let shiftRight = d >> 1 // 0b0101

Range

Range is a type in Swift that represents a range of values. It is used to iterate over a sequence of values or to access a subset of elements in an array, string, or other collection. Swift provides three types of ranges

Closed Range

A closed range operator includes both the start and end values. For example, 1...5 creates a range that includes the values 1, 2, 3, 4, and 5.

let closedRange = 1...5 
// includes 1, 2, 3, 4, 5

Half-Open Range

A half-open range operator includes the start value but excludes the end value. For example, 1..<5 creates a range that includes the values 1, 2, 3, and 4.

let halfOpenRange = 1..<5 
// includes 1, 2, 3, 4
// Iterate over a closed range
for i in 1...5 {
    print(i)
}

// Slice an array using a half-open range
let array = [1, 2, 3, 4, 5]
let slice = array[1..<4] // [2, 3, 4]

// Use a range in a switch statement
let value = 3
switch value {
case 1...5:

One-Sided Range

A one-sided range is used when you want to include all the elements from the start of a collection or up to a certain index. Swift provides two types of one-sided ranges:

Closed Range from the Start (..<b):

This range includes all the elements from the start of a collection up to, but not including, the specified index.

Closed Range through the End (a...):

This range includes all the elements from the specified index to the end of the collection.

Follow for more

Linkedin: https://www.linkedin.com/in/prahladinala/
Github: https://github.com/prahladinala/
Instagram: https://instagram.com/prahlad.inala/
Twitter: https://twitter.com/prahladinala
Figma Community: https://www.figma.com/@prahladinala
Dribbble: https://dribbble.com/prahladinala
Behance: https://www.behance.net/prahladinala
Personal Portfolio: https://prahladinala.in
ToolMate: https://toolmate.co.in

Thank you!

Did you find this article valuable?

Support Prahlad Inala by becoming a sponsor. Any amount is appreciated!

ย