Custom Search
 
  

 
Parentheses Rule

There are cases where the precedence rule may cause a problem. For example:

The BASIC expression LET Y = A/B+ C would produce undesired results, because A would be divided by B and the result added to C. The solution to this problem is in the use of parentheses. If we let parentheses override the order of precedence (but maintain the order of precedence within the parentheses), the result will be satisfactory.  Now lets examine the previous example using parentheses.

Example:

Using parentheses

With the parentheses, B is added to C and the sum of this operation is divided into A, giving the correct result.

Sometimes more than one set of parentheses may be needed to tell the BASIC language in what order to execute the arithmetic operations.

Example:

Parentheses inside parentheses

The BASIC expression to accomplish this would be:

For this expression to give us the correct results, A and B must be added first, then the sum divided by C, and finally that result is squared. When parentheses within parentheses are used, the innermost parentheses will be evaluated first. Addition has a lower precedence than either division or exponentiation; therefore, A + B must be in the inner parentheses. Division has a lower precedence than exponentiation, so (A+B)/C also must be enclosed in parentheses, ((A+B)/C), to ensure it is performed next.

The important thing to remember is the parentheses maybe used to over-ride the normal order of precedence. The parentheses rule says:

    . Computations inside parentheses are performed first.

    . If there are parentheses inside parentheses, the operations inside the inner pair are performed first.

CONSTANTS AND VARIABLES

Throughout Chapters 2 and 3 we have been using constants and variables.

In the BASIC language, we have two ways to refer to a numeric value: first, by a numeric-constant representing the value, and second, by an arbitrary name, a numeric-variable name, representing the value.

Numeric-constants

A numeric-constant is a decimal number, whose value does not change. It may be a whole number or have a decimal or fractional part. If it has a fractional part it must be expressed as a decimal number such as 3.5 rather than 3 1/2. If the number gets too large, the system software will convert the number to scientific notation. As described in Chapter 2, this is simply a decimal fraction multiplied by a positive or negative power of ten.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business