COBOL/Conditions

Conditional Statements in COBOL
In computer science, conditional statements—and the related terms conditional expressions and conditional constructs—are features of a programming language which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false. This is typically achieved by selectively altering the control flow based on some condition. Conditional statements are branching instructions.

COBOL includes the IF-THEN-ELSE conditional construct, and the EVALUATE construct. Each is controlled by the truth-value (True of False) of some logical expression, often based on a relations condition.

In general, a relational condition has the form: [Data Name/Arithmetic Operation] [IS] [NOT] [Equal to (=),Greater than (>), Less than (<), Greater than or Equal (>=), Less than or equal (<=) ] [Data Name/Arithmetic Operation]

Here are some examples: X IS > 5 X IS NOT EQUAL TO 0 X IS < 0

Logical (Boolean) operators can be used to combine relational expressions. These include AND, OR, NOT.

Here are some examples: X IS > 5 AND Y IS < 0 X IS > 5 OR Y IS <0

A typical IF-THEN-ELSE construct is: IF Relations Expression (is TRUE) THEN Execute these statements ELSE Execute these statements END-IF

We see this in the IF-ELSE approach section of the example program.

COBOL also includes an EVALUATE statement which is a multiple branch construct, similar to the switch statement or Case statement used in several modern languages. This is used in conjunction with a WHEN clause where the relational condition is specified. The TRUE parameter selects the subsequent WHEN clauses that contain relational tests that evaluate as true.

We see the EVALUATE WHEN construct in the Evaluate approach section of the example program.

The Conditions lesson in the Wikiversity course Programming Fundamentals provides additional background, readings, multimedia resources, key terms, and activities related to understanding and using functions.

Please study those materials to better understand and extend the following example program.

Program Notes
This example provides two alternative problem solutions in the same program listing. The first uses the IF-ELSE conditional and the second uses the EVALUATE verb. Remove one of these alternatives before running the program.

For example, to run the IF-ELSE approach, remove the lines from the EVALUATE statement through the END-EVALUATE statement. Alternatively, remove the lines from the IF CHOICE statement through the END-IF Statement.

The EVALUATE verb provides for multiple branching, similar to the switch statement that often appears in newer languages. The TRUE parameter selects the subsequent WHEN clauses that contain relational tests that evaluate as true.

Try It
Copy and paste the code above into one of the following free online development environments or use your own compiler / interpreter / IDE.
 * JDoodle
 * paiza.IO
 * TutorialsPoint

Assignment
Part 1:

The kelvin is the base unit of temperature in the International System of Units (SI), having the unit symbol K. It is widely used in scientific work.

Modify the example program listed above to convert Fahrenheit and Celsius temperatures to and from Kelvins. Wikipedia provides a table of temperature conversions useful for this exercise.

Run and test the program in at least one programming environment.

Part 2:

In elementary algebra, the quadratic formula is a formula that provides the solution(s) to a quadratic equation.

Given a general quadratic equation of the form


 * $$ax^2+bx+c=0$$

with $x$ representing an unknown, $a$, $b$ and $c$ representing constants with $a ≠ 0$, the quadratic formula is:


 * $$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\ \ $$

where the plus-minus symbol "±" indicates that the quadratic equation has two solutions. Written separately, they become:


 * $$ x_1=\frac{-b + \sqrt {b^2-4ac}}{2a}\quad\text{and}\quad x_2=\frac{-b - \sqrt {b^2-4ac}}{2a}$$

Each of these two solutions is also called a root (or zero) of the quadratic equation. Geometrically, these roots represent the $x$-values at which any parabola, explicitly given as $y = ax^{2} + bx + c$, crosses the $x$-axis.

Modify the example program to compute the quadratic formula. This assignment requires using the COBOL SQRT function which has the form:

FUNCTION SQRT (argument)

Run and test the program in at least one programming environment.

Use test cases that result in two distinct real roots, a double root, and two complex roots.