Swift - Comparison Operators



Comparison Operator in Swift

Comparison operators are the most frequently used operators in Swift. They are used to compare two values or expressions and return a boolean value accordingly. They are commonly used with loops and conditional statements. Swift supports the following comparison operators −

Operator Name Example
== Equal to 56 == 56 = true
!= Not Equal to 56 != 78 = true
> Greater than 56 > 32 = true
< Less than 44 < 67 = true
>= Greater than or Equal to 77 >= 33 = true
<= Less than or Equal to 21 <= 56 = true

Equal to Operator in Swift

The equal to operator is used to check if both the given values are equal or not. If they are equal, then it will return true. Otherwise, it will return false.

Syntax

Following is the syntax of the Equal to "==" operator −

Value1 == Value2

Example

Swift program to check if the input password is equal to the stored password using the equal to "==" operator.

import Foundation

let password = "XP123"

if (password == "XP123"){
   print("Welcome!! Entered password is correct")
} else {
   print("Error!!! Please enter correct Password")
}

Output

Welcome!! Entered password is correct

Not Equal to Operator in Swift

The not equal to operator is used to check if both the given values are not equal. If they are not equal, then it will return true. Otherwise, it will return false.

Syntax

Following is the syntax of the not equal to operator −

value1 != value2

Example

Swift program to check if the input string is not equal to the stored string using the not equal to(!=) operator.

import Foundation
let str = "Mohina"

if (str != "Noni"){
   print("Both the strings are not equal")
} else {
   print("Both the strings are equal")
}

Output

Both the strings are not equal

Greater than Operator in Swift

The greater than operator is used to check if the left-hand side value is greater than the right-hand side value. If they are, then it will return true. Otherwise, it will return false.

Syntax

Following is the syntax of the Greater than operator −

value1 > value2

Example

Swift program to add only those numbers that are greater than 50 using greater than ">" operator.

import Foundation

let arr = [3, 55, 2, 44, 66]
var  sum  = 0

// Iterate through each element of the array
for x in arr{
   // Find the sum of only those elements that 
   // are greater than 50
   if (x > 50){
      sum += x
   }
}
print("Sum = \(sum)")

Output

Sum = 121

Less than Operator in Swift

The less than operator is used to check if the left-hand side value is less than the right-hand side value. If they are, then it will return true. Otherwise, it will return false.

Syntax

Following is the syntax of the Less than operator −

value1 < value2

Example

Swift program to add only those numbers that are less than 55 using less than(<) operator.

import Foundation

let arr = [1, 55, 2, 90, 12]
var  sum  = 0

// Iterate through each element of the array
for x in arr{
   // Find the sum of only those elements that 
   // are less than 55
   if (x < 55){
      sum += x
   }
}
print("Sum = \(sum)")

Output

Sum = 15

Greater than or Equal to Operator in Swift

The greater than or equal to operator is used to check if the left-hand side value is greater than or equal to the right-hand side value. If they are, then it will return true. Otherwise, it will return false.

Syntax

Following is the syntax of the Greater than or Equal to operator −

value1 >= value2

Example

Swift program to check valid age for voting using greater than or equal to(>=) operator.

import Foundation
let age = 18

if (age >= 18){
   print("You are eligible for voting")
} else {
   print("You are not eligible for voting")
}

Output

You are eligible for voting

Less than or Equal to Operator in Swift

The less than or equal to operator is used to check if the left-hand side value is less than or equal to the right-hand side value. If they are, then it will return true.

Syntax

Following is the syntax of the Less than or Equal to operator −

value1 <= value2

Example

Swift program to compare two numbers using less than or equal to operator.

import Foundation
let num = 18
if (num <= 20){
   print("Given num is less than 20")
} else {
   print("Given num is greater than 20")
}

Output

Given num is less than 20
swift_operators.htm
Advertisements