Operators in Swift



 
Operators in Swift


Operators in Swift are symbols or keywords that perform operations on variables and values. Here's an overview of different types of operators in Swift:


 1. Arithmetic Operators


Arithmetic operators perform basic mathematical operations.


 Addition (+): Adds two numbers together.

  swift

  let sum = 10 + 5  // sum equals 15

  


 Subtraction (): Subtracts the second number from the first.

  swift

  let difference = 10  5  // difference equals 5

  


 Multiplication (): Multiplies two numbers.

  swift

  let product = 10  5  // product equals 50

  


 Division (/): Divides the first number by the second.

  swift

  let quotient = 10 / 5  // quotient equals 2

  


 Remainder (%): Returns the remainder of the division.

  swift

  let remainder = 10 % 3  // remainder equals 1

  


 2. Logical Operators


Logical operators perform operations on Boolean values.


 Logical AND (&&): Returns true if both operands are true.

  swift

  let result = (true && false)  // result equals false

  


 Logical OR (||): Returns true if at least one operand is true.

  swift

  let result = (true || false)  // result equals true

  


 Logical NOT (!): Inverts the Boolean value.

  swift

  let result = !true  // result equals false

  


 3. Comparison Operators


Comparison operators compare two values and return a Boolean result.


 Equal to (==): Checks if two values are equal.

  swift

  let isEqual = (5 == 5)  // isEqual equals true

  


 Not equal to (!=): Checks if two values are not equal.

  swift

  let isNotEqual = (5 != 3)  // isNotEqual equals true

  


 Greater than (>): Checks if the left operand is greater than the right operand.

  swift

  let isGreater = (10 > 5)  // isGreater equals true

  


 Less than (<): Checks if the left operand is less than the right operand.

  swift

  let isLess = (5 < 10)  // isLess equals true

  


 Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.

  swift

  let isGreaterOrEqual = (10 >= 10)  // isGreaterOrEqual equals true

  


 Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.

  swift

  let isLessOrEqual = (5 <= 10)  // isLessOrEqual equals true

  


 4. Assignment Operators


Assignment operators assign values to variables.


 Assignment (=): Assigns the value on the right to the variable on the left.

  swift

  var age = 30

  


 Compound Assignment (+=, =, =, /=, %=): Performs an operation on the variable and assigns the result to the variable.

  swift

  var count = 10

  count += 5  // count now equals 15

  


Swift's operators allow for efficient computation and logical evaluation, enabling developers to perform a wide range of operations from basic arithmetic to complex logical decisions.

Comments

Popular posts from this blog

Complete iOS Developer Guide - Swift 5 by @hiren_syl |  You Must Have To Know 😎 

Debugging

Swift Fundamentals You Can’t Miss! A List by @hiren_syl