Activity 1
Calculate a discounted price
Given a price of 80.0 and a discount percentage of 15.0, calculate the final price and interpolate it into a sentence.
Hint: Convert the percentage to a fraction before multiplying.
Swift Fundamentals
Combine, compare, and transform values while producing readable text output.
Before you begin
Learning stage
Operators perform work on values. Arithmetic operators calculate results, comparison operators produce Boolean values, and logical operators combine or invert conditions.
String interpolation embeds an expression inside a string using backslash followed by parentheses. Swift evaluates the expression and inserts its textual representation into the surrounding string.
Learning stage
This example calculates lesson progress and builds a status message from the result.
let completed = 7
let total = 10
let percentage = Double(completed) / Double(total) * 100
let isComplete = completed == total
let canContinue = completed < total && completed > 0
print("Progress: \(completed)/\(total) — \(percentage)%")
print("Complete: \(isComplete)")
print("Can continue: \(canContinue)")Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Given a price of 80.0 and a discount percentage of 15.0, calculate the final price and interpolate it into a sentence.
Hint: Convert the percentage to a fraction before multiplying.
Activity 2
Create a Boolean that is true only when a user is signed in and has either a paid plan or an active trial.
Hint: Use parentheses to make the OR group explicit.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
3
Integer division discards the fractional remainder. Convert operands to Double when a fractional result is needed.
QUESTION 2
It evaluates the expression and inserts its result into the string.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: = assigns a value, while == asks whether two values are equal.
The assignment operator changes or initializes storage and doesn't return a Boolean comparison result. The equality operator requires comparable operands and evaluates to true or false, so it can be used in conditions.
Possible follow-ups
Learning stage
Operators turn values into decisions and results. Keep expressions readable, convert numeric types deliberately, and prefer interpolation for messages that include values.