Bird
Raised Fist0
Swiftprogramming~10 mins

Comparison operators in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Comparison operators
Start
↓
Evaluate Left Value
↓
Evaluate Right Value
↓
Apply Comparison Operator
↓
Result: true or false
↓
Use Result in Condition or Expression
↓
End
The program compares two values using a comparison operator and produces true or false as the result.
Execution Sample
Swift
let a = 5
let b = 10
let result = a < b
print(result)
This code compares if a is less than b and prints the Boolean result.
Execution Table
StepActionEvaluationResult
1Assign a = 5a = 55
2Assign b = 10b = 1010
3Compare a < b5 < 10true
4Assign result = trueresult = truetrue
5Print resultprint(true)true
💡 Comparison done and result printed; program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aundefined5555
bundefinedundefined101010
resultundefinedundefinedundefinedtruetrue
Key Moments - 2 Insights
Why does the comparison a < b result in true?
Because at step 3 in the execution_table, a is 5 and b is 10, and 5 is less than 10, so the comparison returns true.
Is the result variable storing a number or a Boolean?
At step 4, result stores the Boolean value true, not a number, because comparison operators always return true or false.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the result of the comparison a < b?
Atrue
Bfalse
C5
D10
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the variable 'result' assigned a value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column for assignment to 'result' in the execution_table.
If we change 'a' to 15, what would be the result of 'a < b' at step 3?
A15
Btrue
Cfalse
D10
💡 Hint
Compare 15 < 10 logically, referencing the evaluation in step 3.
Concept Snapshot
Comparison operators in Swift compare two values.
They return true or false.
Common operators: <, >, <=, >=, ==, !=.
Used in conditions and expressions.
Example: let result = a < b
Result is a Boolean.
Full Transcript
This visual execution shows how comparison operators work in Swift. First, variables a and b are assigned values 5 and 10. Then the comparison a < b is evaluated, which checks if 5 is less than 10. This returns true. The result variable stores this Boolean value. Finally, the program prints true. Comparison operators always return true or false, which can be used in conditions or stored in variables.

Practice

(1/5)
1. Which of the following comparison operators checks if two values are equal in Swift?
easy
A. ==
B. !=
C. >
D. <=

Solution

  1. Step 1: Understand the equality operator

    The operator == is used to check if two values are equal in Swift.
  2. Step 2: Compare with other operators

    != means not equal, > means greater than, and <= means less than or equal to.
  3. Final Answer:

    == -> Option A
  4. Quick Check:

    Equality operator = == [OK]
Hint: Remember: double equals means equality check [OK]
Common Mistakes:
  • Confusing == with = (assignment)
  • Using != to check equality
  • Mixing > and < with equality
2. Which of the following is the correct syntax to check if variable a is not equal to variable b in Swift?
easy
A. a != b
B. a =! b
C. a !== b
D. a <> b

Solution

  1. Step 1: Identify the not equal operator in Swift

    Swift uses != to check if two values are not equal.
  2. Step 2: Check syntax correctness

    a != b is correct. a =! b is invalid syntax, !== is for identity comparison in some languages but not for value inequality in Swift, and <> is not a Swift operator.
  3. Final Answer:

    a != b -> Option A
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Not equal always has ! before = [OK]
Common Mistakes:
  • Swapping = and ! in != operator
  • Using === instead of !=
  • Using <> which is not valid in Swift
3. What will be the output of the following Swift code?
let x = 10
let y = 20
print(x > y)
medium
A. true
B. 10
C. 20
D. false

Solution

  1. Step 1: Understand the comparison

    The code compares if x (10) is greater than y (20).
  2. Step 2: Evaluate the comparison

    Since 10 is not greater than 20, the expression x > y is false.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    10 > 20 = false [OK]
Hint: Compare values carefully, > means strictly greater [OK]
Common Mistakes:
  • Assuming 10 > 20 is true
  • Confusing > with <
  • Printing variable values instead of boolean
4. Identify the error in the following Swift code snippet:
let a = 5
let b = 10
if a = b {
    print("Equal")
} else {
    print("Not Equal")
}
medium
A. Using print instead of println
B. Missing parentheses around condition
C. Using = instead of == in if condition
D. Variables a and b must be var, not let

Solution

  1. Step 1: Check the if condition syntax

    The condition uses a = b, which is assignment, not comparison.
  2. Step 2: Correct operator for comparison

    To compare values, Swift requires == instead of =.
  3. Final Answer:

    Using = instead of == in if condition -> Option C
  4. Quick Check:

    Comparison needs ==, not = [OK]
Hint: Use == for comparison, = is assignment [OK]
Common Mistakes:
  • Using = instead of == in conditions
  • Thinking parentheses are mandatory in Swift if
  • Confusing print and println functions
5. You want to check if a number n is between 10 and 20 inclusive in Swift. Which of the following expressions correctly uses comparison operators to do this?
hard
A. n > 10 || n < 20
B. n >= 10 && n <= 20
C. n == 10 && n == 20
D. n != 10 && n != 20

Solution

  1. Step 1: Understand the range condition

    To check if n is between 10 and 20 inclusive, it must be greater than or equal to 10 and less than or equal to 20.
  2. Step 2: Analyze each option

    n >= 10 && n <= 20 uses n >= 10 && n <= 20, which correctly checks the inclusive range. n > 10 || n < 20 uses OR which is incorrect for range. n == 10 && n == 20 checks if n equals both 10 and 20 simultaneously, which is impossible. n != 10 && n != 20 checks if n is not 10 and not 20, which does not ensure the range.
  3. Final Answer:

    n >= 10 && n <= 20 -> Option B
  4. Quick Check:

    Inclusive range uses >= and <= with AND [OK]
Hint: Use && with >= and <= for inclusive range checks [OK]
Common Mistakes:
  • Using || instead of && for range
  • Checking equality to both ends at once
  • Using != which excludes values instead of range
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard