FAQ: Variables - Compound Assignment Operators

This community-built FAQ covers the “Compound Assignment Operators” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Swift

FAQs on the exercise Compound Assignment Operators

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


Another customer purchases half of the available apples.

Underneath your previous code, change the value of apples to reflect this exchange using a compound assignment operator.

I initially added apples %= 50 but it printed 12. I understand the exercise wanted me to divide by 2, but if there are already 12 apples, why does 50% give me 12?

apples -%= 50 gives an error and apples %= -50 also gives me 12.

If I replace apples %= 50 with %= 5 it prints 2… What is going on here?

Dear byte,

On Swift, “%” doesn’t mean percentage, it means remaining. Let’s remember that in this case, apples = 12.

So essentially, what your code (apples %= 50) did was:
12 apples divided by 50, and then show the remaining of that equation.
So if we are talking about int numbers, you cannot divide 12 by 50, and the remaining will always be 12.

When you write (apples %= 5), you are doing the following:
12 divided by 5, will result in 2 and the remaining will be 2.
So when you print it, it will show 2.

However, if you want to show the actual percentage, you will have to use some math.
There are a couple of ways to show how much is 50% of 12 is.
(Solution 1)
First, we have 12 divided by 2. The result will be 6, or essentially 50%:
apples /= 2
The result will be 6.

Alternatively, we could multiply 12 by 0.5. The result will be 6 (which would mean 50%).
That’s, in my opinion, the most practical way.
If you want 20% of something, multiply it by 0.2
If you want 85% of something, multiply it by 0.85
If you want 100% of something, multiply it by 1
If you want 150% of something, multiply it by 1.5
And so on.

*For this specific scenario, only solution 1 would work, as our variant type is Int and not double.

Hope I was clear. Feel free to ask any questions. :slight_smile: