0
0
Swiftprogramming~15 mins

Comparison operators in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Comparison Operators in Swift
📖 Scenario: You are helping a small shop owner compare prices of two products to decide which one is cheaper or if they cost the same.
🎯 Goal: Build a simple Swift program that compares two product prices using comparison operators and shows the result.
📋 What You'll Learn
Create two variables with exact prices
Create a variable to hold a comparison threshold
Use comparison operators to compare the prices
Print the comparison result clearly
💡 Why This Matters
🌍 Real World
Comparing prices helps shop owners decide which product to promote or buy.
💼 Career
Understanding comparison operators is essential for decision-making logic in software development.
Progress0 / 4 steps
1
Create price variables
Create two variables called priceA and priceB with values 19.99 and 24.99 respectively.
Swift
Need a hint?

Use var to create variables and assign the exact decimal values.

2
Add a price difference threshold
Create a variable called threshold and set it to 0.01 to allow small price differences.
Swift
Need a hint?

This threshold helps to consider prices equal if they differ by less than this amount.

3
Compare the prices using comparison operators
Use if statements with ==, <, and > operators to compare priceA and priceB considering the threshold. Create a variable result that stores the string "Prices are equal", "Price A is cheaper", or "Price B is cheaper" accordingly.
Swift
Need a hint?

Use abs() to find the difference and compare with threshold. Assign the correct message to result.

4
Print the comparison result
Print the value of the variable result to show the comparison outcome.
Swift
Need a hint?

Use print(result) to display the message.