0
0
Swiftprogramming~10 mins

Comparison operators in Swift - Step-by-Step Execution

Choose your learning style9 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.