02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Numbers And Basic Math

As long as we pass a string inside the trace functions such as trace("I am a string, baby!");, the output is the string itself. If we pass a number instead of a string inside the quotes, the result is still the same i.e., the number which was inputted. But when we write the number into a trace function without double quotes or single codes, the result is different. For example trace(200);, since 200 is not written inside double quotes, it is not considered as string. It is considered as live data, in our case a number. Hence, any modifications can be performed inside the parenthesis and the final answer is displayed on the output panel as flash will automatically convert the number into a string just before it triggers the function.

Simple mathematical Operations

Let's consider a simple math equation written inside trace in this way: trace(200-100);. Instead of printing the output as 200-100, the output is '100'. This happens because before the value is sent to the trace function – it will first calculate the operation (200-100=100). Only when the final value is reached will it be sent through to the output panel. All the basic math operations (of any length) can be performed by Flash.

Level of priority for math symbols

As the length of an equation increases, do does the difficulty for calculating it. This is because various symbols (+,-.) are used in an equation and no priority for symbols is specified to the compiler by the user. (When we refer to symbol priority, we are discussing the order in which those symbols are calculated in an expression such as 'x + y * z'. What is done first? x+y or yz? It depends on the symbol priority.) Flash uses the same level of symbol priory that all programing languages do. In general, multiplication and division have the highest priority while addition and subtraction have the least.

For example, if we consider 10-32+5 and start simply evaluating from left to right, the answer would be 19, which is wrong. Not being aware of the symbol priority can produce unexpected/incorrect results. Hence, we must follow the basic math principles and evaluate high priority symbols first: in this case, multiplication () before addition. The proper equation becomes 10-6+5. Since all the rest of the symbols have the same priority, the evaluation can be started either from left or right or even from the middle. The answer remains the same and is always correct . Flash follows the basic priority level rules of math. If you don't realize this factor you'll find yourself getting confused often - if your match skills aren't very high its not the end of the world: instead you just use round braces() to gather mathematical equations into groups and help you visualize this rule (for example (10-(3*2) +5)).

There may be a few instances where we want the evaluation to start at a certain part irrespective of the priority. This is also possible with the help of (), which has a higher priority than multiplication and division. The part of equation that needs to be evaluated first can be included in the parenthesis and therefore be evaluated first. However, after completing the evaluation inside the parenthesis, Flash again follows math basic priorities to evaluate the rest of the equation. In some cases, if the part that needs to be evaluated inside the parenthesis is big, Flash follows the same priority principles for evaluating that part inside the parenthesis.

This might look little confusing, but when you start working, you'll quickly get used to it (our exams will help you a lot with it as well.) Take some time now and use trace to solve some equations for you, such as

trace(4+5*8-3); //can you guess the answer before you compile these?

trace((4+5)*8-3);

Video Transcript

00:00With numbers in Flash, they're working very similarly
00:04to the way you would expect them from any calculator.
00:08And the way it works is I'm going to just do a trace,
00:11because we're going to put our values inside of our trace.
00:14And inside of our trace, which don't forget
00:16to open and close a round bracket
00:18and end your line with a semicolon.
00:21And inside of our bracket, I'm just
00:23going to type a number, for example, 300.
00:27If I save my application, I wouldn't run it.
00:30We're going to see in our output 300.
00:32But this 300 is a number.
00:35It's not a string.
00:36If I would wrap it around a string
00:39by putting it inside of a double quote or a quote,
00:42then it wouldn't be a number, it would be a string.
00:45But because I'm not wrapping it, it's a live data.
00:50So because it's live data, I could actually
00:52take this 300 and do something to that number
00:55before I send it to the trace panel.
00:57For example, I could subtract from 300, 200.
01:01So I could type 300 minus 200.
01:05And if I do that and I run my application,
01:07Flash is going to first do the math, remove 200 from the 300,
01:16and then output the value.
01:19Now the same comes to duplication and division and plus.
01:24So let's create a little sample that does all of those.
01:28So I'm going to take the number 1,
01:29and I'm going to duplicate it by 10.
01:32I'll be using the Shift-A to create my duplicate.
01:37And I'm going to type minus 4 divide
01:43by 2, which divide is the slash.
01:47Now don't type double slash, because if you type a double
01:50slash, then it's actually, it means you want to quote it out.
01:56You don't want that.
01:57As you can see, it's grayed out.
01:59It's not part of your code anymore.
02:01And it would generate an error.
02:02So you want to only have one division.
02:06So now that we have one division, if we do the math here,
02:08we're going to see that 1 times 10 is 10.
02:11And 4 divide by 2 is 2, because duplication and division
02:15happen before plus and minus.
02:18So we would get 10 minus 2, which should come out as 8.
02:25And something's wrong here.
02:28And the thing that's wrong here is I didn't save the file.
02:31Very important to save the file.
02:32So I'm going to click on Command-S
02:34and run the application one more time.
02:36And we got the 8 that we were expecting to get,
02:40which is a very important thing.
02:41I'm doing this automatically, saving it.
02:43And I'm forgetting to say save.
02:46Every time before you publish the file,
02:48you want to make sure that you're saving,
02:49because if not, it's going to compile the last thing that
02:51was saved.
02:53And when I'm compiling, just a quick reminder,
02:55we're doing it with Command-Enter.
02:57And if you forgot the shortcut, you
02:59could always pop up to the Control
03:02and see what your shortcut is.
03:06Very good.
03:06So now we started that we could do division.
03:08We could do plus.
03:10We could do minus.
03:11We could do duplicate.
03:12And we know that the order is the basic order from the basic
03:17order of math, where duplication and division
03:19comes before minus and plus.
03:22So the order of math is the exact same order
03:26as we would expect it.
03:27Now, what would happen if we would add brackets, which
03:30we could do that?
03:31What would happen if we would put 10 minus 4
03:35inside of a bracket?
03:37That changes the rules a bit, because brackets
03:39come before division and duplication.
03:43So brackets are in the top of the order.
03:46First, everything inside of the bracket happens.
03:50There's nothing new here.
03:51This is math basics.
03:54So first, whatever happens, we first
03:55are going to take what's inside of our bracket.
03:58We could have many brackets here.
04:00After that is done, then we're running through the application
04:03one more time.
04:07In other words, if I just come down here one times,
04:12let me just copy this.
04:17So what would happen here?
04:18This would basically flash will first decrypt
04:22what's inside of the bracket, which comes down to 6.
04:25And then this is the value that's going to come by.
04:28Now it's going to go by the order,
04:30because everything is straight here.
04:31So everything is in the same level of priority.
04:34Division and duplication is in the same level of priority.
04:37Then it's going to go 1 times 6 divided by 2.
04:40So 1 times 6 is 6.
04:42Divide by 2 is 3.
04:45So if we run the application one more time,
04:47and again, I didn't save the file.
04:49So I'm going to click on Command-S
04:51and run the application one more time.
04:53And we could see that our value is 3, as we expected.
04:59So now I'm going to give you a little small challenge.
05:04And I'm going to give you the answer in the next video,
05:07just to get you up and running and thinking
05:10before we publish the file.
05:12Now, it's not important to be math masters
05:17to do programming.
05:18Most programmers, I know, barely know math.
05:23There's some hardcore architectural,
05:27software engineers.
05:28And if you go to school to learn computers,
05:33probably 90% of the courses are about mathematics.
05:36But then it's just, when you're coming down
05:39to working with Flash, most of the math that's involved
05:41is just basic math.
05:43And it's more math of logic and less
05:47about complex mathematics.
05:49There are, of course, some brilliant developers
05:51that have that knowledge.
05:53But it's not mandatory to create quite amazing things
05:56inside of Flash.
05:57But it is mandatory to get grasp basic mathematics
06:01to be able to do very cool things inside of Flash.
06:05Now, with that said, I want to leave you with a quick equation
06:08and see if you could calculate this in your head
06:11and see that everything makes sense.
06:13And we're going to make this the most complex one so far.
06:19Just start typing here, along.
06:31Try to see if you could figure out this value
06:33without publishing it.
06:36And in the next video, we're going
06:37to find out what the value is.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!