Swift Programming - Zero To Hero - Part Five

Introduction

This is part five of "Swift Programming - Zero to Hero" series. You can read the previous articles in the series.

  1. http://www.c-sharpcorner.com/article/swift-programming-zero-to-hero-part-three/
  2. http://www.c-sharpcorner.com/article/swift-programming-zero-to-hero-part-four/
In this article, we will learn about Operators in Swift.
  • Unary Operator
  • Multiplicative Operator
  • Additive Operator
  • Shift Operator
  • Relational and Type testing
  • Equality Operator
  • Logical AND
  • Logical XOR
  • Logical OR
  • Conditional AND
  • Conditional OR 
  • Multiplicative Operator

What Are Operators?

Operators are nothing but special symbols that are used to specify some computation or other operations, such as Arithmetic and Logical Operations on the operands. Therefore, operators specify the operations to be performed in the expression. There are several operators in swift as listed above and let us look in detail.

Primary Operators

Square Brackets a[x] this specifies that Square Brackets can be used within Arrays.

Post-Increment Operator x++, Increases the value of its operands (x) by 1. It first assigns the values of x and then increments its value.

Post-Decrement Operator x--, Decreases the value of its operands (x) by 1. It first assigns the values of x and then decrements its value.

Unary Operators

Unary Plus Operator+, this operator is predefined for all numeric types and the result of a unary + operation on a numeric type is always the value of the operand.

Unary Minus Operator- This operator is predefine for all numeric types and the result of a unary - operation on a numeric type is always the value of the operand.

Logical Negation operator:! this operator Inverts the result of a Boolean expression.

Bitwise Complement Operator:`, this operator inverts the binary representation of an expression

Pre-Increment Operator: ++x, this operator Increases the value of its operand (x) by 1. The pre-increment operation returns the value of the operand (x) after it has been incremented.

Pre-Decrement Operator:--x, this operator Decreases the value of its operand (x) by 1. The pre-decrement operation returns the value of the operand (x) after it has been decremented.

Multiplicative Operators

Multiplication Operator: *, this operator computes the product of its operands.

Division Operator: /, this operator computes the division of its operands by dividing the first operand by the second operand.

Modulus Operator: %, this operator computes the remainder of its operands after dividing the first operand by the second operand.

Additive Operators

Binary Plus Operator:+, this operator computes the sum of its operands.

Binary Minus Operator:-, this operator computes the difference of its operands.

Shift Operators

Left Shift Operator: <<, this operator, shifts its first operand to the left by the number of bits specified by the second operand. The number of bits should be of an integer value.

Right shift Operator: >>, this operator, shifts its first operand to the right by the number of bits specified by the second operand. The number of bits should be of an integer value.

Relational and Type Testing Operators

Less than operator : < , finds out if the first operand is less than the second operand. It returns true if the condition satisfies; otherwise returns false.

Greater than operator :>, finds out if the first operand is greater than the second operand. It returns true if the condition satisfies; otherwise returns false.

Less than equal to operator: <=, finds out if the first operand is less than or equal to the second operand. It returns true if the condition satisfies; otherwise returns false.

Greater than equal to operator :>=, finds out if the first operand is greater than or equal to the second operand. It returns true if the condition satisfies; otherwise returns false.

Equality Operator

Equality Operator: ==, Checks whether the two expressions are same or not. It returns true if the expressions are equal; otherwise it returns false.

Inequality Operator:!=, Checks whether the two expressions are same or not. It returns true if the expressions are not equal; otherwise it returns false.

Logical Operators

Logical AND: &, Computes the logical AND of its Boolean operands. For integral operands, (&) the logical AND operator compares the corresponding bits of two integrals and computes the logical bitwise AND of the operands. It returns true if and only if both of its operands are true. It can be used as a unary or binary operator.

Logical XOR: ^, Computes the logical exclusive, OP of its Boolean operands. It returns true, if and only if one of its operands is true.

Logical OR:|, Computes the logical OR of its Boolean operands. For integral operands, | it compares the corresponding bits of two integrals and computes the logical bitwise OR of the operands. It returns true if and only if, any one of its operands is true.

Conditional AND: &&, Used to compute the logical AND of its Boolean operands. It evaluates the second operand whenever necessary and returns true, if and only if both its operands are true.

Conditional OR: ||, Computes the logical OR of its Boolean operands. It evaluates the second operand whenever necessary and returns true, if and only if any of its operands is true.

Conclusion

In this Article we have learned some of Operators. In next article we can learn about Assignment Operators with Examples. Hope this article was useful.


Similar Articles