18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>SIMSCRIPT</strong> <strong>II.5</strong> Basic Concepts<br />

ever the logical intent of a program is not immediately evident from its <strong>SIMSCRIPT</strong> <strong>II.5</strong> instruction<br />

sequences. The use of comments will be illustrated in many of the example programs below.<br />

1.22 Some Sample <strong>SIMSCRIPT</strong> <strong>II.5</strong> Level 1 Programs<br />

The following programs illustrate the <strong>SIMSCRIPT</strong> <strong>II.5</strong> concepts and statements presented in this<br />

chapter. The programs are printed as they might appear before being submitted for compilation.<br />

The flexibility of <strong>SIMSCRIPT</strong> <strong>II.5</strong> makes many program statement formats possible. Those used<br />

here are examples only.<br />

1.22.1 Roots of a Quadratic Expression<br />

The following example, demonstrating the use of nested if statements, calculates the roots of a<br />

quadratic equation, which may be expressed in the general form:<br />

AX 2 + BX + C = 0<br />

First, the coefficients, A, B, and C, are read from input data. They are printed so that they may be<br />

checked for any input errors. The coefficients are then tested for the trivial zero, constant, or linear<br />

cases. Otherwise, the roots, alpha and beta, may be calculated from the familiar formulae:<br />

alpha = -B + (B**2 - 4AC)**(1/2)<br />

2A<br />

beta = -B - (B**2 - 4AC)**(1/2)<br />

2A<br />

Before obtaining the square root, the sign of the sub-expression B 2 - 4AC is evaluated. If this value<br />

is less than zero, the roots have complex values; otherwise, real values. Notice how the print<br />

statement, as used in the example, can be used to print both the program title and the labeled variable<br />

names.<br />

31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!