AIntroduction
The operator is a special symbol or phrase used to check the values or changing the values. The addition operator adds two numbers together. Swift supports most standard c operators and to eliminate common coding errors. Arithmetic operators detect and disallow value overflow and to avoid unexpected results when working with numbers.
Swift allows remainder calculations on floating-point numbers and provide two range operators.
Unary operator
It operates on a single target. Unary prefix operators appear before the target and unary postfix operators appear after the target.
Binary operator
It operates on two targets and infix and appears between two targets.
Ternary operator
It operates on three targets and Swift has only a ternary operator.
Assignment Operator
The assignment operator initialize or update the value and if the right side of the assignment is a tuple with multiple values its elements decomposed into multiple constants. The assignment operator in swift does not return a value.
let b = 10
var a = 5
a = b
Arithmetic operator
Swift has four arithmetic operators like addition, subtraction, multiplication, and division. Swift arithmetic operator does not allow value to overflow by default and it supports the string operation in it.
1 + 2
"hello" + "world"
Remainder Operator
The remainder operator is represented as modulo operator and to determine the solution for a % b the % operator calculates the below equation a= (b * multiplier )+ remainder. the multiplier must be the largest number of multiples of b. The same method is used to calculate the remainder for a negative value.
9 % 4
Increment and Decrement operator
Swift provides an increment operator ++ and a decrement operator to increase or decrease the value of a numeric variable by 1. The operator with variables of any integer or floating-point type is used. The ++ and -- symbol is used as a prefix operator or postfix operator. If the operator is before the variable, it increments the variable before returning its value or if the operator is after the variable, it increments the variable after returning its value.
- var a = 0
- let b = ++a
-
-
-
- let c = a++
-
-
Unary Minus Operator
The unary minus operator is prepended directly before the value it operates without any white space and the unary plus operator simply return the value it operates on without any change: The unary plus operator does not do anything and it is used to provide symmetry in the code for positive numbers and using unary minus operator for negative numbers.
- let three = 3
- let minusThree = -three
-
-
- let plusThree = -minusThree
-
-
- let minusSix = -6
- let alsoMinusSix = +minusSix
-
Compound Assignment operator
Swift provides a compound assignment operator that combines assignment = with another operation. The expression a += 2 is shorthand for a = a+2.The addition and assignment are combined into one operator that performs both tasks at the same time and the operator does not return a value.
Comparison Operator
Swift support standard c comparision operators like equal to, not equal to, greater than, Less than, Greater than, Greater than or equal to, less than or equal to and each comparision operators return a Bool value to indicate the statement is true or not. It is mainly used in conditional statement to check conditions.
- let name = "world"
- if name == "world" {
- println("hello,world")
- }
- else {
- println("\(Name) not match")
- }
-
Ternary Conditional Operator
The ternary operator with three parts and it is a shortcut for evaluating one of two expression based on question is true or false. If question is true it return value or it evaluate its value. The ternary operator is shorthand for tha code below,
- if question {
- answer1
- } else {
- answer2
- }
Logical operator
Logical operators modify or combine the boolean logic values true and false. Swift supports three standard logical operators namely Logical Not, Logical AND, Logical OR.
Logical NOT
It inverts a Boolean value so true becomes false and false becomes true. The logical NOT operator is a prefix operator and it appears before the value it operates on without whitespace.
- let allowedEntry = false
- if !allowedEntry
- {
- println ("ACCESS DENIED")
- }
Logical AND
The logical operator creates logical expressions where both values must be true for overall expression to also be true. The below example consist of two bool values and allows access if both are true,
- let enteredDoorCode = true
- let passedRetinaScan = false
- if enteredDoorCode && passedRetinaScan {
- println(""
- Welcome ")
- }
- else {
- println("ACCESS DENIED")
- }
-
Logical OR
The logical OR operator is an infix made from two adjacent pipe characters. It is used to create logical expressions only one of two values has to be true for overall expression be true. The logical OR operator uses a short circuit evaluation to consider its expressions.