Comparison Operators
Part of the Fundamentals section of Coddy's Swift journey — lesson 20 of 86.
Comparison operators let you compare two values and determine their relationship. Unlike arithmetic operators that produce numbers, comparison operators always return a Bool — either true or false.
Swift provides six comparison operators:
let a = 10
let b = 5
print(a == b) // false (equal to)
print(a != b) // true (not equal to)
print(a > b) // true (greater than)
print(a < b) // false (less than)
print(a >= b) // true (greater than or equal to)
print(a <= b) // false (less than or equal to)These operators work with numbers, strings, and other comparable types. For strings, Swift compares them alphabetically:
print("apple" < "banana") // true
print("cat" == "cat") // trueComparison operators are essential for making decisions in your code. You'll use them constantly when checking conditions, validating input, or determining which action to take based on values.
Challenge
EasyYou are provided with the following variables:
let x = 15
let y = 20
let z = 15Use comparison operators to evaluate and print the result of each comparison below, in this exact order:
- Is
xequal toz? - Is
xnot equal toy? - Is
ygreater thanx? - Is
xless thanz? - Is
ygreater than or equal to20? - Is
xless than or equal to10?
Print each result on a separate line.
Cheat sheet
Comparison operators compare two values and return a Bool (true or false).
Swift provides six comparison operators:
let a = 10
let b = 5
print(a == b) // false (equal to)
print(a != b) // true (not equal to)
print(a > b) // true (greater than)
print(a < b) // false (less than)
print(a >= b) // true (greater than or equal to)
print(a <= b) // false (less than or equal to)Comparison operators also work with strings, comparing them alphabetically:
print("apple" < "banana") // true
print("cat" == "cat") // trueTry it yourself
let x = 15
let y = 20
let z = 15
// TODO: Write your code below
// Use comparison operators to evaluate each comparison and print the results
// 1. Is x equal to z?
// 2. Is x not equal to y?
// 3. Is y greater than x?
// 4. Is x less than z?
// 5. Is y greater than or equal to 20?
// 6. Is x less than or equal to 10?This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input