NOTE
Mr. Gordon generated this concept summary and the related exercises using ChatGPT (here are the prompts given and responses received).
He then edited the results for clarity, accuracy, and brevity.
Arithmetic Operators
Swift supports the standard arithmetic operators found in most programming languages:
+(Addition): Adds two values together.
Example:2 + 3 // Result: 5-(Subtraction): Subtracts one value from another.
Example:5 - 2 // Result: 3*(Multiplication): Multiplies two values.
Example:4 * 3 // Result: 12/(Division): Divides one value by another.
Example:10 / 2 // Result: 5%(Remainder): Returns the remainder of division, when using integers.
Example:9 % 4 // Result: 1
On a related note, in Swift, the remainder(dividingBy:) method for the Double data type computes the remainder when a value is divided by another Double. This method returns the remainder of the division, keeping the sign of the original value.
Here is the basic syntax:
let remainder = someDouble.remainder(dividingBy: anotherDouble)Here is a simple example:
let number = 14.0
let divisor = 2.5
let result = number.remainder(dividingBy: divisor)
print("Remainder: \(result)") // Output: Remainder: 1.5In this example, 14.0 divided by 2.5 leaves a remainder of 1.5.
Assignment Operators
=(Assignment): Assigns a value to a variable.
Example:let a = 5assigns the value5to the constanta.
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%=: Modulo and assign
Example:var f = 9 f %= 4 // Result: f is now 1
Exercises
Create a new Xcode playground named Operators to complete these exercises.
You can do these regular exercises (immediately below), or skip ahead and do the Taylor Swift themed exercises. I know what I’d pick! 🎶
1: Simple Arithmetic
- Use variables to store the values
10and3. - Perform the following operations with the two variables you created, and print the results:
- Addition
- Subtraction
- Multiplication
- Division
- Remainder
2: Updating Values
- Create a variable called
scoreand set its value to50. - Use the assignment operators (
+=,-=,*=,/=,%=) to update the value ofscorewith different numbers. Print the results after each update.
3: Combining Operators
- Create a variable called
resultand assign it an initial value of100. - Use a combination of arithmetic and assignment operators to perform the following tasks:
- Subtract 20 from
result. - Divide
resultby 2. - Multiply
resultby 5. - Take the remainder of
resultdivided by 7.
- Subtract 20 from
4: Calculating Total Cost
- Imagine you’re shopping. Start with a variable called
totalCostset to0.0. - Add the following item costs to
totalCostusing the+=operator:- $19.99
- $25.50
- $8.75
- Print the final total.
5: Remainders
- You are baking cookies and have
12.5cups of flour. Each batch of cookies requires exactly2.75cups of flour. Write a program to calculate how much flour will be left over after making as many full batches of cookies as possible. Use theDoubletype and theremainder(dividingBy:)method.
Exercises: Taylor’s Version
1: Simple Arithmetic
“Addition is the best thing that’s ever been mine!”
- 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.
- Use variables to store the values
10(U.S. concerts) and3(international concerts). - Perform the following operations and print 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)”
- Taylor has 27 original songs on her Fearless (Taylor’s Version) album, but she’s been writing more!
- Create a variable called
totalSongsand set its value to27. - Use the assignment operators (
+=,-=,*=,/=,%=) to update the value oftotalSongsas 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!”
- Create a variable called
toursCompletedand assign it the value3(representing Taylor’s completed tours). - Update
toursCompletedto 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!”
- Taylor is tracking her concert ticket sales. She starts with a variable called
totalTicketsSoldset to0. - Add the following ticket sales to
totalTicketsSoldusing 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.
- Print the final total of tickets sold.
5: Remainders
“The Long Concert Break”
Taylor is planning her next world tour. She wants to perform a series of very specific 3.5-hour concerts, but she has a total of 27.8 hours available for concerts.
Write a program to calculate the remainder of hours Taylor will have after performing as many full concerts as possible using the Double type and the remainder(dividingBy:) method.