Next freebie in 28 Hours*
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.
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.
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 y*z? 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-3*2+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?
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.