Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Perform Order of Operations

James Thomas
James Thomas
6,452 Points

8*7+8%5+6/2 to me this == 59.6. Apparently I am wrong.

Apparently I am wrong. Please explain?

Justin Coberly
Justin Coberly
16,929 Points

Normally the % (modulo) operator has the same level of precedence as multiplication and division. So in this statement we can evaluate it from left to right.

8*7 = 56 8%5 = 3 <- the remainder is the answer to a modulus expression 6/2 = 3 56+3+3 = 62

5 Answers

Steven Parker
Steven Parker
231,269 Points

You're forgetting about operator precedence.

All multiplicative operations (which include multiplication, division, and modulus) are performed before any additive opertions ( + or - ).

I'll bet you can figure it out now without a spoiler. :wink:

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Ok so for Computer Science the Order of Operations is:

  1. P ---> Parentheses
  2. E ---> Exponents
  3. M ---> Multiplication
  4. D ---> Division (This is for programming only, division holds the same weight as multiplication
  5. M ---> Modulos (This is for programming only, and it can has the same order weight as division and multiplication)
  6. A ---> Addition
  7. S ---> Subtraction

So by following these rules we know that our addition will be done last and our multiplication, division, and Modulos will be done first, so this could have effectively been written as (8*7) + (8%5) + (6/2)

We know that 8 * 7 = 56 We know that 8 % 5 = 3 We know that 6 / 2 = 3

So when we add them all together we get 56 + 3 + 3 ----- or ----- 56 + 6 = 62

Steven Parker
Steven Parker
231,269 Points

There's no common rule for all of "computer science", operator precedence varies with language. For example, in C#, multiplication does not take precedence over division — so 12/2*3 is 18, not 2.

See the reference page I linked in the title of my answer for more information.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

That is because of associativity, so you are correct there! I realized that I have not specified that division and modulos hold the same weight as multiplication so I will fix that.

Steven Parker
Steven Parker
231,269 Points

By the C# rules, the associativity of multiplicative operations is left-to right. That's why 12/2*3 evaluates to 18, instead of 2.

I recommend following the rules of the language you are programming in. If there are "general rules" that the language is "breaking", it doesn't seem like they would be useful to the developer and they could lead to confusion and unexpected program behavior.

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

I see where you are coming from, I have a math background so I always see that there are general rules (keeps us sane) but in programming I have noticed that some languages seem to break this (for some reason or another).

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

PEMDMAS does apply in Python where it's more like PE[MDM][AS]

>>> 12 / 2 * 3
18.0  # not 2
>>> 12 / 4 % 2 * 3
3.0  # / then % then *
>>> 25 % 13 / 2 * 3
18.0 # % then / then *
>>> 15 - 3 + 2
14  # not 10
Justin Coberly
Justin Coberly
16,929 Points

Normally the % (modulo) operator has the same level of precedence as multiplication and division. So in this statement we can evaluate it from left to right.

8*7 = 56 8%5 = 3 <- the remainder is the answer to a modulus expression 6/2 = 3 so... 56+3+3 = 62

James Thomas
James Thomas
6,452 Points

Excellent answers, thanks all. Sometimes learning the write software it's difficult to see the wood for the trees but this has been explained well!

Umut İnanç
PLUS
Umut İnanç
Courses Plus Student 818 Points

I like to put paranteses between numbers which are going to be multiplied, divided or modulated to seperate additive and multiplicative operations to make it easier:

(8*7)+(8%5)+(6/2)

This way you can easily solve it.

(56)+(3)+(3) = 62