Locked lesson.
About this lesson
We review PEMDAS and how the Order of Operations works within C#.
Exercise files
Download this lesson’s related exercise files.
15 - Math Order Of Operations.docx61.1 KB 15 - Math Order Of Operations SOLUTION.docx
58 KB
Quick reference
Math Order Of Operations
Order of Operations refers to doing math in a specific order.
When to use
Use this any time you need to use math in your program.
Instructions
Math order of operations is:
PEMDAS:
- Parenthesis
- Exponents
- Multiplication and Division
- Addition and Subtraction
Hints & tips
- PEMDAS is named after the first letters of each of the math operations: Parenthesis, Exponents, Multiplicate and Division, Addition and Subtraction.
- 00:04 Okay, in this video, we're going to look at math order of operations.
- 00:06 And I get a real kick out of this because I don't think hardly anybody in society
- 00:11 understands order of operations.
- 00:13 You see these little quizzes on Facebook all the time,
- 00:15 where there's a long string of numbers and they say, what's the answer?
- 00:19 And there'll be like 3 million responses, and everyone's got it wrong.
- 00:23 And people argue with each other.
- 00:24 No, it should be this.
- 00:25 No, it should be that.
- 00:27 And it all comes down to basic arithmetic, order of operations.
- 00:31 And people forgot how to do order of operations from their grade school days in
- 00:35 early math.
- 00:35 So that's what we're going to look at in this video.
- 00:38 So let's just start out here.
- 00:39 Let's go say three, and I'm not even going to use variables.
- 00:42 I'm just going to type in numbers.
- 00:43 So 3 plus 4, let's say,
- 00:47 times 3 minus 4 plus 2.
- 00:51 I don't know.
- 00:52 So, what do you think the answer is going to be for this?
- 00:56 So let me just kind of put this in here.
- 00:59 And let's go through here.
- 00:59 Now, most people would start from left to right.
- 01:03 So they would go 4 plus 4, well, that's 7.
- 01:06 And then we've got times 3 minus 4 plus 2.
- 01:12 So we would then continue left to right again, 7 times 3.
- 01:17 Well, that's 21.
- 01:18 We know that, right?
- 01:19 Minus 4 plus 2.
- 01:22 And then continuing on left to right 21 minus 4, that's what 17.
- 01:27 And then plus 2, and then we come to our answer of 19.
- 01:32 Now most people would think that that makes sense.
- 01:34 But that is absolutely wrong.
- 01:36 The answer is not 19 at all.
- 01:38 Our answers should be, let me see, I think 13, it looks like.
- 01:41 Let's go ahead and run this and see.
- 01:46 Yep, sure enough, 13.
- 01:48 So fun tip, if you ever see one of those Facebook quizzes,
- 01:50 pull up your Visual Studio and punch it in here to get the actual answers so
- 01:54 you can know what the real answer is.
- 01:56 But the reason why this is not 19 is because this is not how
- 01:58 you do order of operations.
- 02:00 You don't go left to right like this, right?
- 02:03 So wrong!
- 02:05 What we do is something called, well, there's different sort of acronyms or
- 02:09 whatever that word is to visualize these things.
- 02:12 The thing I learned was PEMDAS.
- 02:15 And it's different in different countries.
- 02:17 But PEMDAS stands for parentheses, is that how you spell parentheses, exponents,
- 02:24 multiplication, division, addition, and subtraction.
- 02:29 So PEMDAS, P-E-M-D-A-S.
- 02:36 And that means you start with parentheses first.
- 02:39 If there are parentheses, do those.
- 02:42 Next, if there are exponents, you do those.
- 02:44 If there are no exponents, you do multiplication.
- 02:46 If there are no multiplication, you do division.
- 02:48 If there's no division, you do addition and subtraction.
- 02:52 And then you go left from right doing these things.
- 02:55 So in our answer here, again, let's go through here.
- 02:58 So using PEMDAS, what do we have?
- 03:02 First, you do parentheses.
- 03:03 That's not how you spell parentheses.
- 03:04 Well, there are no parentheses there.
- 03:06 Well, next, let's do exponents.
- 03:07 Well, there's no exponents here.
- 03:08 Next, let's do multiplication.
- 03:10 4 times 3.
- 03:12 We could do that.
- 03:13 So let's go 3 plus, what's 4 times 3?
- 03:18 12, we've got 12 minus 4 plus 2.
- 03:23 So now we start over again.
- 03:25 Is there a parentheses?
- 03:25 Nope. Is there exponents?
- 03:26 Nope.
- 03:27 Is there multiplication?
- 03:28 Nope.
- 03:29 Is there division?
- 03:29 Nope. Is there addition?
- 03:31 Yes, there is, 3 plus 12.
- 03:33 So 3 plus 12 we know is 15, minus 4,
- 03:37 plus 2, and we start all over again.
- 03:41 Left to right. Are there parentheses?
- 03:43 Nope. Exponents?
- 03:44 Nope. Multiplication?
- 03:44 Nope. Division?
- 03:45 Nope. Addition?
- 03:45 Nope. Subtraction?
- 03:46 Yep, 15 minus 4.
- 03:48 Well, that's 11 plus 2.
- 03:52 And now, finally, we start again.
- 03:55 Is there parentheses, exponents, multiplication, division, addition?
- 03:58 Yep! We got addition, 11 plus 2, 13.
- 04:02 Which if we save this and run it, like we should already, sure enough 13.
- 04:08 So, remember your PEMDAS order of operations, sort of important.
- 04:12 I mean, not so important because C# is going to do all this for you.
- 04:17 But good to know anyway,
- 04:19 if you're building a program where you need to know the order of operations.
- 04:24 So this is the exact same as this, right?
- 04:29 So if we save this and run it,
- 04:31 we're still going to get 13 because the order of operations hasn't changed.
- 04:36 Now we could start out here, while there are parentheses.
- 04:38 So whatever's inside the parentheses that gets done first.
- 04:41 Well, that's the way we did it the last time anyway
- 04:43 when there weren't parentheses.
- 04:44 Because multiplication is the next one down in the PEMDAS list.
- 04:48 So it kind of works out that way.
- 04:50 But if you had a different line of numbers,
- 04:52 you would always do the parentheses first.
- 04:54 Exponents would be next multiplication, division, addition, subtraction.
- 04:58 So that's math order of operations.
- 05:00 And you are now capable of solving all of those Facebook quizzes correctly
- 05:05 every time.
- 05:05 Hopefully, you don't need to use C# for that.
- 05:08 But if you do, punch the numbers and get the correct answer.
- 05:10 And it's a lot of fun, so that's all for this video.
- 05:12 In the next video, we'll look at floats versus ints.
Lesson notes are only available for subscribers.