Getting started

Create a new Xcode playground named Operators to try out the examples below, and, later, the exercises.

Arithmetic operators

Swift supports the standard arithmetic operators found in most programming languages.

TIP

Try each example out in your Xcode Playground to verify the result.

To do this with a code block, hover your mouse over the code block, then click the copy button .

You can then paste the code into your Playground using Command-V.

  • + (Addition): Adds two values together.
     2 + 3
     // Result: 5
  • - (Subtraction): Subtracts one value from another.
     5 - 2
     // Result: 3
  • * (Multiplication): Multiplies two values.
     4 * 3
     // Result: 12
  • / (Division): Divides one value by another.
     9 / 4
     // Result: 2 (Four goes into nine twice)
  • % (Remainder): Returns the remainder of division, when using integers.
     9 % 4
     // Result: 1 (Remainder after two groups of four removed from nine)

Assignment operators

  • = (Assignment): Assigns a value to a variable.
     var a = 5
     // Assigns the value 5 to the variable a

The assignment operator can be combined with arithmetic operators to update a variable’s value:

  • +=: Add and assign
    Example: 
     var b = 3
     b += 2 // Result: b is now 5  
  • -=: Subtract and assign
    Example: 
    var c = 8
    c -= 3 // Result: c is now 5
  • *=: Multiply and assign
    Example: 
    var d = 4
    d *= 2 // Result: d is now 8
  • /=: Divide and assign
    Example:
    var e = 10
    e /= 2 // Result: e is now 5
  • %=: Find remainder and assign
    Example:
    var f = 9
    f %= 4 // Result: f is now 1

Exercises

You can do the un-themed exercises (immediately below), skip ahead and do the Taylor Swift themed exercises, or do both sets of exercises for additional practice.

I know what I’d pick! 🎶

1: Simple Arithmetic

  1. Use variables to store the values 10 and 3.
  2. Perform the following operations with the two variables you created, and verify the results using the sidebar, at right, in your Xcode Playground:
    • Addition
    • Subtraction
    • Multiplication
    • Division
    • Remainder

2: Updating Values

  1. Create a variable called score and set its value to 50.
  2. Use the assignment operators (+=-=*=/=%=) to update the value of score with different numbers. Verify the results after each update.

3: Combining Operators

  1. Create a variable called result and assign it an initial value of 100.
  2. Use a combination of arithmetic and assignment operators to perform the following tasks:
    • Subtract 20 from result.
    • Divide result by 2.
    • Multiply result by 5.
    • Take the remainder of result divided by 7.

4: Calculating Total Cost

  1. Imagine you’re shopping. Start with a variable called totalCost set to 0.0.
  2. Add the following item costs to totalCost using the += operator:
    • $19.99
    • $25.50
    • $8.75
  3. Verify the final total using the results sidebar at right.

Exercises: Taylor’s Version

1: Simple Arithmetic

“Addition is the best thing that’s ever been mine!”

  1. Taylor just finished her Eras Tour and wants to tally up some numbers. She performed 10 concerts in the U.S. and 3 concerts internationally.
  2. Use variables to store the values 10 (U.S. concerts) and 3 (international concerts).
  3. Perform the following operations and verify the results:
    • How many total concerts did Taylor perform?
    • If Taylor had to cancel 4 concerts from the total, how many would be left?
    • If each U.S. concert had 50,000 attendees, how many total attendees were there for the U.S. shows?
    • If Taylor sold 120,000 total tickets for her international shows and you divide them equally between the 3 concerts, how many tickets were sold per show?
    • If Taylor released 9 versions of her 1989 album and numbered them, what would be the remainder if you divide the total number by 4?

2: Updating Values

 “It’s a Love Story (in numbers)”

  1. Taylor has 27 original songs on her Fearless (Taylor’s Version) album, but she’s been writing more!
  2. Create a variable called totalSongs and set its value to 27.
  3. Use the assignment operators (+=-=*=/=%=) to update the value of totalSongs as Taylor adds or removes songs:
    • Taylor writes 10 more songs.
    • She removes 5 songs from the set list for her next concert.
    • She triples the number of songs to record different versions for different countries.
    • She decides to split the total number of songs equally across 3 albums.
    • She wonders how many songs would be left if she tried to fit the total into sets of 6.

3: Combining Operators

“Shake it up!”

  1. Create a variable called toursCompleted and assign it the value 3 (representing Taylor’s completed tours).
  2. Update toursCompleted to reflect the following:
    • Taylor announces 2 more tours next year.
    • Due to scheduling, she cancels 1 tour.
    • She multiplies her total tours by 2 as she plans to perform each one twice.
    • Taylor divides her tours equally across 4 continents.
    • She wants to know how many tours would be left over if she grouped them into sets of 3.

4: Ticket Sales

“Ticketmaster, look what you made me do!”

  1. Taylor is tracking her concert ticket sales. She starts with a variable called totalTicketsSold set to 0.
  2. Add the following ticket sales to totalTicketsSold using the += operator:
    • 65,000 tickets sold for her show in Nashville.
    • 80,000 tickets sold for her show in Los Angeles.
    • 72,000 tickets sold for her show in New York.
  3. Verify the final total of tickets sold.

Partial solutions

Here are a set of solutions to some of the exercises above. Please review, comparing these to your own work. Ask questions of Mr. Gordon through your portfolio on Notion using @Russell Gordon as needed. You will see questions like these on Monday’s mini-test. Note that the mini-test on Monday will be written on paper – no computers or other reference material permitted.