
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift - Range Operators
Swift provides range operators, such as a..<b and a...b that can be used as a shortcut for expressing a range of values. These operators are generally used with a for-in loop. There are three types of range operators are available in Swift:
Operator | Name | Example |
---|---|---|
(ab) | Closed Range | 14 return 1, 2, 3, 4 |
(a..<b) | Half-Open Range | 1..<3 return 1, and 2 |
(a) and (a) | One-Sided Range | (1..) return 1, 2, 3, end of the element (2) return beginning to 2 |
1. Closed Range Operator
A closed range operator is used to define a range that contains both starting and end values. In other words, a closed range is used to create a range from x to y, including the value of x and y. Here the starting value or value of x must not be greater than the ending value or value of y.
Syntax
Following is the syntax of the closed range operator −
xy
Example
Swift program to display a sequence of numbers using a closed range operator.
import Foundation // For loop to display a sequence of numbers for x in 3...11{ print(x) }
The output of the above example is:
3 4 5 6 7 8 9 10 11
2. Half-Open Range Operator
A half-open range operator is used to create a range from x to y. This range only includes the starting value and does not include the ending value or y. Also, the value of x or the starting value should not be greater than the ending value of y.
Or if the value of x is equal to y, then the range will be empty. This operator is generally used with zero-based lists like arrays, etc.
Syntax
Following is the syntax of the half-open range operator −
x..<y
Example
Swift program to display the elements of an array using a half-open range operator.
import Foundation let arr = [1, 4, 6, 2, 7] for x in 0..<arr.count{ print(arr[x]) }
The output of the above example is:
1 4 6 2 7
3. One-Sided Ranges
The one-sided ranges are used to define ranges that have a value of only one side. Such types of ranges are used when we need to know from where the iteration should begin. One-sided ranges are of two types −
Partial Range from Start − It defines a range that starts from the beginning of the given collection or sequence and goes to the specified end value. This range contains only the end value and does not contain the starting value.
Syntax
Following is the syntax of partial range from start −
x or ..<x
Partial Range to the End − It creates a range that starts from the specified value and goes all the way to the end of the given collection or sequence. This range contains only the starting value and does not contain the end value.
Syntax
Following is the syntax of partial range to the end −
x
Example
Swift program to demonstrate partial range to the start.
import Foundation let arr = [1, 4, 6, 2, 7] // This one-sided range displays elements till index 3 print("Sequence 1:") for x in arr[...3]{ print(x) } // This one-sided range displays all the elements before index 3 print("Sequence 2:") for y in arr[..<3]{ print(y) }
The output of the above example is:
Sequence 1: 1 4 6 2 Sequence 2: 1 4 6
Example
Swift program to demonstrate partial range to the end.
import Foundation let arr = [1, 4, 6, 2, 7] // This one-sided range displays all elements // starting from index 2 to the end of the sequence print("Sequence:") for x in arr[2...]{ print(x) }
The output of the above example is:
Sequence: 6 2 7
Access Array Elements Using Swift Range Operators
The range operators can be used to access array elements. You can use these operators inside the square brackets [] with the array name to access the particular range of the elements.
The syntax to access array elements using range operator is:
array_name [lower_bound...upper_bound]
Example
Swift program to access array elements using range operators.
import Foundation // Array of names let names = ["Aarav", "Diya", "Kabir", "Meera", "Vivaan", "Anaya", "Ishaan"] // Accessing a range of elements using closed range operator let selectedNames = names[1...4] for name in selectedNames { print(name) }
The output of the above example is:
Diya Kabir Meera Vivaan
Important Points to Remember
Remember these important points while working with range operators:
- The lower bound value must be less than or equal to the upper bound when using a closed range (e.g.,
1...5
). - Range operators work with both positive and negative values, but they do not reverse the order of iteration. If the lower bound is greater than the upper bound, the range will be invalid and result in a runtime error.