Skip to content
Prepare to Grow

Swift Fundamentals

Operators and String Interpolation

Combine, compare, and transform values while producing readable text output.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Use arithmetic, comparison, and logical operators.
  • Distinguish assignment from equality.
  • Build readable strings with interpolation.
  • Recognize operator precedence and use parentheses intentionally.
01

Learning stage

Concept Explanation

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.

  • = assigns a value; == compares two values.
  • && requires both conditions to be true.
  • || requires at least one condition to be true.
  • ! inverts a Boolean condition.
02

Learning stage

Syntax & Examples

This example calculates lesson progress and builds a status message from the result.

Progress.swiftswift
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)")
Expected values: 70.0%, false, and true.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Calculate prices, percentages, dimensions, pagination offsets, and progress.
  • Compare state before deciding whether to show, disable, or update interface elements.
  • Use interpolation for labels, diagnostic messages, accessibility text, and formatted summaries.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Using = when the intention is to compare with ==.
  • Performing integer division when a fractional result is required.
  • Writing long expressions whose precedence is difficult to understand.
  • Building readable messages through repeated string concatenation instead of interpolation.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

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.

Activity 2

Combine conditions

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.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

What does 7 / 2 produce when both operands are Int values?

  1. A.3
  2. B.3.5
  3. C.4
  4. D.A String
Reveal answer

3

Integer division discards the fractional remainder. Convert operands to Double when a fractional result is needed.

QUESTION 2

What is the purpose of \(expression) inside a Swift string literal?

Reveal answer

It evaluates the expression and inserts its result into the string.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

How do = and == differ in Swift?

Short answer: = assigns a value, while == asks whether two values are equal.

Explore the detailed answer

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

  • • What does === compare?
  • • Why can integer division surprise beginners?
08

Learning stage

Key Takeaways

Operators turn values into decisions and results. Keep expressions readable, convert numeric types deliberately, and prefer interpolation for messages that include values.

  • Assignment and equality serve different purposes.
  • Comparison and logical operators produce Bool values.
  • Integer division remains integer division.
  • Parentheses can communicate intent even when precedence is known.