Showing posts with label Reminder. Show all posts
Showing posts with label Reminder. Show all posts

Thursday, 5 January 2017

Basic Operators in Swift



Basic Operators

 An operator is a special symbol or phrase that use to check, change, or combine values. For example, the addition operator (+) adds two number. 
let a = 2 + 3
Swift supports most standard C operators and improves several capabilities to eliminate common coding errors.

Unlike C, Swift lets you perform remainder (%) calculations on floating-point numbers. Swift also provides two range operators (a..<b and a...b) not found in C, as a shortcut for expressing a range of values. 


Terminology


Operators are unary, binary, or ternary:
  • Unary operators operate on a single target (such as -a, ++i, --i).
  • Binary operators operate on two targets (such as 2 + 3) and are infix because they appear in between their two targets.
  • Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).
The values that operators affect are operands. In the expression 1 + 2, the + symbol is a binary operator and its two operands are the values 1 and 2. 

Assignment Operator(=)

Assignment operator initializes or updates the value.
let x = 100   // Declare a  constant variable x with value 100.
var y = 5   //// Declare a  variable y with initial value 5.
y = x        // y is equal to x(100)

Arithmetic Operators

Swift supports the four standard arithmetic operators for all number types:

1. Addition (+)   
   1 + 5   //equals 6
2. Subtraction (-) 
   14 - 4   //equals 10
3. Multiplication (*) 
   4*7    //equals 28 
4. Division (/) 
   30/5    //equals 6
Unlike the arithmetic operators in C and Objective-C, the Swift arithmetic operators do not allow values to overflow by default.


Remainder Operator


The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder).
  9 % 4 // equals 1 
To determine the answer for a % b, the % operator calculates the following equation and returns remainder as its output:
a = (b x some multiplier) + remainder
where some multiplier is the largest number of multiples of b that will fit inside a.


Increment and Decrement Operators

Like C, Swift provides an increment operator (++) and a decrement operator (--) as a shortcut to increase or decrease the value of a numeric variable by 1.

var x = 0;
++x      //Now x equal to 1.
 
Each time you call ++i, the value of i is increased by 1. Essentially, ++i is shorthand for saying i = i + 1. Likewise, --i can be used as shorthand for i = i - 1.
The ++ and -- symbols can be used as prefix operators or as postfix operators. ++i and i++ are both valid ways to increase the value of i by 1. Similarly, --i and i-- are both valid ways to decrease the value of i by 1. 

1. If the operator is written before the variable, it increments the variable before returning its value.  
2. If the operator is written after the variable, it increments the variable after returning its value.

Unary Minus Operator

The sign of a numeric value can be toggled using a prefixed -, known as the unary minus operator: 
let a =10
let minus_a = -a   //minus_a equal to -10
let plus_a = -minus_a  //plus_a equal to 10
The unary minus operator (-) is prepended directly before the value it operates on, without any white space.

Unary Plus Operator

The unary plus operator (+) simply returns the value it operates on, without any change:

 let minus_value = -10
 let sec_value = +minus_value    //sec_value is equal to -10

Compound Assignment Operators

Like C, Swift provides compound assignment operators that combine assignment (=) with another operation. One example is the addition assignment operator (+=): 

Ex : let a = 5;
       a +=4     //now  a equal to 9

The expression a += 2 is shorthand for a = a + 2. Effectively, the addition and the assignment are combined into one operator that performs both tasks at the same time. 
NOTE: The compound assignment operators do not return a value. You cannot write let b = a += 2.

Comparison Operators

Swift supports all standard C comparison operators: 
  1. Equal to (==) 
   2.  Not equal to (!=)
   3. Greater than  (>)
   4. Less than (<)
   5.Greater than or equal to(>=)
   6.Less than or equal to(<=)

NOTE: Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance.

Ternary Conditional Operator

The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value.

a==5?b:c
if a equal to 5 than its return b otherwise c. 


Nil Coalescing Operator

The nil coalescing operator (x ?? y) unwraps an optional x if it contains a value, or return a default value y if x is nil. The expression x is always an optional type. The expression y must match the type that is stored inside x.

NOTE: If the value of a is non-nil, the value of b is not evaluated. This is known as short-circuit evaluation. 
EX: let firstColor = "Black"
      let defaultColor = "White"
      let backgroundColor = firstColor ?? defaultColor
     //In this example value of background color is Black


      let firstColor : String?

      let defaultColor = "White"
      let backgroundColor = firstColor ?? defaultColor
     //In this example value of background color is White because firstColor is nil

Range Operators

Swift includes two range operators, which are shortcuts for expressing a range of values.

Closed Range Operator

The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b.
The closed range operator is useful when iterating over a range in which you want all of the values to be used, such as with a for-in loop:
 for index in 1...5{
          println ("\(index) times 5 is \(index * 5)")
}
OutPut:
// 1 times 5 is 5 
// 2 times 5 is 10 
// 3 times 5 is 15  
// 4 times 5 is 20
// 5 times 5 is 25   





Half-Open Range Operator

The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b. It is said to be half-open because it contains its first value, but not its final value. As with the closed range operator, the value of a must not be greater than b. If the value of a is equal to b, then the resulting range will be empty.
Half-open ranges are particularly useful when you work with zero-based lists such as arrays, where it is useful to count up to (but not including) the length of the list: 

for index in 1..<5{
          println ("\(index) times 5 is \(index * 5)")
}
OutPut:
// 1 times 5 is 5 
// 2 times 5 is 10 
// 3 times 5 is 15  
// 4 times 5 is 20 

Logical Operators

Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:
 1. Logical Not Operator(!)
      Logical not invert boolean value so that true become false and false become true.

2.Logical And Operator (&&)
     The logical AND operator (a && b) creates logical expressions where both values must be true for the overall expression to also be true.
If either value is false, the overall expression will also be false. In fact, if the first value is false, the second value won’t even be evaluated, because it can’t possibly make the overall expression equate to true. This is known as short-circuit evaluation. 

3. Logical OR Operator (||)

The logical OR operator (a || b) is an infix operator made from two adjacent pipe characters. You use it to create logical expressions in which only one of the two values has to be true for the overall expression to be true.
Like the Logical AND operator above, the Logical OR operator uses short-circuit evaluation to consider its expressions. If the left side of a Logical OR expression is true, the right side is not evaluated, because it cannot change the outcome of the overall expression.