Lesson Notes and Walkthrough
This lesson shows how variables work together in a realistic mini workflow. Instead of isolated lines, you will see how values flow from input to final result.
Scenario: Simple Shopping Total
Imagine a user adds items to a cart. We store count, price, and discount in variables, then calculate a final total.
var itemCount = 3
var pricePerItem: Double = 12.5
var discount: Double = 5.0
let subtotal = Double(itemCount) * pricePerItem
let total = subtotal - discount
print("Subtotal: \(subtotal)")
print("Total: \(total)")Walkthrough
- Start with user-controlled values (
itemCount,pricePerItem). - Compute intermediate values (
subtotal). - Apply adjustments (
discount). - Print and verify output before moving on.
Why This Pattern Matters
- It separates input values from calculated results.
- It makes bugs easier to find because each value has one clear purpose.
- It scales well when logic gets more complex.