Thursday, January 09, 2020

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators:


  • Closed Range operators 
  • Half-Open Range Operators
  • One-Sided Ranges

Closed Range Operators: 


  • a...b
  • It defines a range starting from a to b. 
  • it includes the values of  a and b
  • value a must not be greather than b

Usage:  for index in 1...5

Half-Open Range Operators: 

  • a..
  • It defines a range starting from a to b. 
  • But it does not include b
  • It is very useful, when working with the arrays. Reason it starts from 0 to count
   Usage: for index in 0..

One Sided Range operators 

Usage
   for name in namesArray[2...] {} it returns index 2 from till the end of array.
   for name in namesArray[...2] {} it returns index 0 from index 2. 
 
Example
let myRange = ...5 
range.contains(4) ==> true 
range.contains(11) ==> false 
range.contains(-4) ==> true

Logical Operators: check my previous blog post 

No comments:

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...