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 

Swift Opertors - Basic Part 2

Comparision Operator

  • Equal To (==)
  • Not Equal (!=)
  • Less than (<)
  • Greater than (>)
  • Less than or Equal to (<=)
  • Greater than or Equal to (>=)
It reruns either true or false, it is used in if conditional statements.

Tubles comparision:  

                 Left to right comparision, if first element same as second then only it goes for second element comparision.

Ternary Conditional Operator:

  • Question ? answer1: answer 2
  • Example answer = 2 < 5 ? true: false

Nil Coalescing Operator: (??)

  • a ?? b
  • It unwraps an optional a if it contains a value, or returns a default value bif a is nil.
  • a is an optional type here
  • b must  match with the type that is stored inside a.





Swift Operators - Basic Part 1

Assignment Operators

  • It initialize or updates the value of a variable with the value of b (a=b)
  • It also supports tuples ((x,y) = (1,2) x -1 and y-2)
  • If x= y {} this is invalid, reason it does not return anything, it just assigns. 

Arithmatic Operators

  • Addition (+)
  • Subtraction (-)
  • Multipication (*)
  • Divisions(/)

Reminder (Modulas) Operator (%) 

  • 7 % 3 = 1  (3*2)+1 = 7 
  • a % b means (b * multipier) + reminder = a 

Unary Minus and Unary Plus:

  •   - a or + a
     Example: a = 3, then -a = -3 and +a = -3
                    +a does not change anything

Compound Assignment opertors

  • It combines assignment operator with another operations.
          Example a += 2 => a = a+2

Swift Operators - Terminology

Terminology

  • Unary Operators: 

    • It operates on a single target.
    • It appears immediately before their target or post their target
                  Example: -a , !a, a!

  • Binary Operators:

    • It operates on two targets
    • It appears in between two targets.
               Example: 2+4, 9*5

  • Ternary Operators

    • It operates on three targets
    • In Swift, it has only one ternary operator.
                   Example:  a ? b: c







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...