How to Check if Two Numbers are Equal in Swift?

Check if Two Numbers are Equal

To check if two numbers are equal, use Relational Equal to Operator.

The syntax to pass the two numbers as operands to the equal to operator is

number_1 == number_2

The expression returns true if number_1 is equal to number_2, else the expression returns false.

Swift Example

In this example, we will take two numbers in number_1 and number_2 and use equal to == operator to check if these two numbers are equal.

main.swift

import Foundation

var number_1 = 4
var number_2 = 4

if (number_1 == number_2) {
    print("number_1 and number_2 are equal.")
} else {
    print("number_1 and number_2 are not equal.")
}

Program Output

number_1 and number_2 are equal.

Summary

Summarising this tutorial, we have learnt how to check if two numbers are equal or not using relational operator – equal to.