Explain What is half-open range operator?

devquora
devquora

Posted On: Feb 22, 2018

 

Half-Open Range Operator: It is an operator (a<b) that is used to defines a range that runs from a to b but it doesn’t contain b. The reason for its name that is half-open is that it does not contains its last value, just have its first value. The value of 'a' compulsory is not greater than b in the closed ranged operator. The resulting range will be empty in the case of a is equal to b.

It is useful when you are working with zero-based lists like arrays where it is useful to calculate the length of the list. For example:

let names = ["Alley", "Anna", "Jack", "Brian"]
let count = names.count
for i in 0..<count span id="docs-internal-guid-b356581b-7fff-3109-a868-e60b14559aae">count
print("Person \(i + 1) is called \(names[i])")
} // Person 1 is called Alley, Person 2 is called Anna, Person 3 is called Jack, Person 4 is called Brian

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Swift Interview Questions

    What is IOS Swift?

    Swift is an advanced language for iOS development, known for its concise and expressive syntax. It enables fast-running software and is used for developing iOS and OS X applications, making it ideal f..

    Swift Interview Questions

    What is Dictionary in Swift?

    In Swift, key-value pairs allow for efficient data storage and retrieval, functioning similarly to hash tables in other programming languages. Accessing values is straightforward by using the correspo..

    Swift Interview Questions

    Explain Enum in Swift?

    In Swift, enums define a common type for a group of related values, enabling type-safe handling within your code. Essentially, an enum groups various related values under the same umbrella, facilitati..