Arithmetic Operators & Expressions



Programming of mathematical formulas is straightforward. The quantity to be evaluated is placed after the = and the result is stored in a variable to the left of the variable. The operators or symbols used to represent a particular evaluation are slightly different from those usually written as is shown below:

OperatorDefinitionSimple Example
+Addsum=36.50+2.33;
-Subtractdiffer=101.0-98.6;
*Multiplyprod=5*9;
/Divideratio=3/5;
%ModuloRemainder in a division: xs=22%5; (modulo=2)


It should be noted that the + operator is not restricted to numbers. It can be used to make a longer string variable from smaller ones, a process termed concatenation, e.g.,

Date="Marc"+"h 15"+", 2000";   leads to
Date="March 15, 2000"


A little carelessness is allowable. If a number is not expressed as a string variable, it will nevertheless be treated as one and be included in the resultant string variable.

More complex equations, such as those expressed algabraicly, require more care in coding since the equation must be written on a line. The most usual problem encountered is with division. For example, it may be desired to evaluate a constant "a" divided by the quantitiy "(b-x)", where "b" is also a constant. If coded carelessly, the wrong answer is obtained. It must be coded as written on paper, parentheses and all.

Wrong answer:   y=a/b-x;
Right answer:    y=a/(b-x);


As a general rule, whenever parentheses occur in a relation do not modify the relation. Just introduce the necessary mathematical operators. The browser "reads" code left-to-right and top-to-bottom. When a mathematical expression is met the evaluation takes place starting with the innermost parentheses first and working outwards. Within parentheses the order of evaluation is the same as if the parentheses were not there. Math function evaluation first, followed in sequence by division, multiplication, and addition or subtraction. The last step in any complex evaluation is a simple sum of terms.

One of the most useful aspects of a programming language is the use of conditional expressions. These are tests to determine whether a number is greater than, less than, or equal to another number. Most commonly these are used in association with "if" or "if...else" statements:

if (condition)
    {Statement(s) 1;}
else
    {Statement(s) 2;}


If the condition is met Statement(s)1 are executed and, when done, the "else" provision is passed over, the program continuing with the next statement. If the condition is not met, Statement(s) 1 are passed over and Statement(s) 2 are executed. It is not necessary to have an "else" provision when an alternative does not exist. It is necessary, though to bracket the alternatives with { and } when there is an "else" provision. "Else" is not a statement to be ended with a semicolon, but an alternative beginning with a { in the JS code. The JS representation of conditionals is shown in the table below:

JS Conditionals
NameSymbolNameSymbol
Equals==Not equal to=
Greater than>Greater than
or equal to
>=
Less than<Less than
or equal to
<=


The usage of conditionals is not restricted to numeric variables. They may be used with alphanumeric strings as well. The location of a character in an ASCII table represents the "weighting" given that character. Thus, non-alphabetic characters come before the numbers 0 to 9. These are before the capital letters A to Z, which in turn are before the lower-case letters a to z. Special characters (alt-keys) come thereafter. This "weighting" means that alphabetical placement can be done in the same manner as numerical rankings.

<script>
if ("Smith"<="Small")
   {document.write("Group 1")}
else
   {document.write("Group 2")}
</script>


Starting with the first character in each name (or number) the browser tests each character in turn. In the example, a difference is not met until the third character in each name. The letter "i" is weighted more than the letter "a" (it occurs later in the alphabet), so the condition lt;= is not met for "Smith" compared to "Small". Hence, program flow passes to the "else" statement and "Group 2" is printed. The "document" is the screen viewed, and "write" amounts to "show the result on the screen".

There is another conditional called a Boolean operator. These are used to tie multiple comparisons into an "if" statement. It is like making up an invited guest list: "Mary" and "Sally" or "Jane" are invited, but not "Joan". To make just this selection the coding would look like this:

<script>
if("Mary"&&"Sally"||"Jane"|"Joan")
   document.write("You're Invited!!)
</script>


These conditionals are presented in the following table:

Boolean Operators
SymbolRepresents
&&"And" (both true)
||"Or" (only 1 true)
|"Not" (negative true=false)


It should be noted that these operators have only two results: true or false. Using them with string variables, as was done, does not usually present a complication. When a string variable is used as the argument in an "if" there are either characters present, "true", or not, "false". With numerics, one does come up. Two values may be the same, i.e., test true, one of two values may be acceptable, testing true, or they may each be unwanted, testing false. What about if one or both values being compared are 0? Are 0's true or false? In this instance, the convention is to assign the test as false. This means occurrences of 0 have to be taken into account so that an "if" or "if...else" does not result in an improper alternative choice.

JS Test-Bed 1 - - JS Test-Bed 2

<< PreviousNext >>