COBOL/Conditions
Conditional Statements in COBOL
[edit | edit source]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.
conditions.cbl
[edit | edit source]*> This program asks the user for a Fahrenheit temperature,
*> converts the given temperature to Celsius,
*> and displays the results.
*>
*> References:
*> https://www.mathsisfun.com/temperature-conversion.html
*> https://www.tutorialspoint.com/cobol/index.htm
*> https://open-cobol.sourceforge.io/doc/gnucobol.html
IDENTIFICATION DIVISION.
PROGRAM-ID. CONDITIONS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CHOICE PIC X.
01 TEMP-IN PIC 999V99.
01 TEMP-OUT PIC ZZ9.99.
01 LABEL-IN PIC X(10).
01 LABEL-OUT PIC X(10).
PROCEDURE DIVISION.
MAIN.
*> MAIN could either be an IF-ELSE structure or an EVALUATE structure
DISPLAY "Enter F to convert to Fahrenheit or C to convert to Celsius:".
ACCEPT CHOICE
*> IF-ELSE approach
IF CHOICE = "C" OR CHOICE = "c" THEN
MOVE "Fahrenheit" TO LABEL-IN
MOVE "Celsius" TO LABEL-OUT
PERFORM GET-TEMPERATURE
PERFORM COMPUTE-CELSIUS
PERFORM DISPLAY-RESULT
ELSE
IF CHOICE = "F" OR CHOICE = "f" THEN
MOVE "Celsius" TO LABEL-IN
MOVE "Fahrenheit" TO LABEL-OUT
PERFORM GET-TEMPERATURE
PERFORM COMPUTE-FAHRENHEIT
PERFORM DISPLAY-RESULT
ELSE
DISPLAY "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
END-IF
END-IF.
*> EVALUATE approach
EVALUATE TRUE
WHEN CHOICE = "C" OR CHOICE = "c"
MOVE "Fahrenheit" TO LABEL-IN
MOVE "Celsius" TO LABEL-OUT
PERFORM GET-TEMPERATURE
PERFORM COMPUTE-CELSIUS
PERFORM DISPLAY-RESULT
WHEN CHOICE = "F" OR CHOICE = "f"
MOVE "Celsius" TO LABEL-IN
MOVE "Fahrenheit" TO LABEL-OUT
PERFORM GET-TEMPERATURE
PERFORM COMPUTE-FAHRENHEIT
PERFORM DISPLAY-RESULT
WHEN OTHER
DISPLAY "You must enter C to convert to Celsius or F to convert to Fahrenheit!"
END-EVALUATE.
STOP RUN.
GET-TEMPERATURE.
DISPLAY "Enter " LABEL-IN " temperature:".
ACCEPT TEMP-IN.
COMPUTE-CELSIUS.
COMPUTE TEMP-OUT = (TEMP-IN - 32) * 5 / 9.
COMPUTE-FAHRENHEIT.
COMPUTE TEMP-OUT = TEMP-IN * 9 / 5 + 32.
DISPLAY-RESULT.
DISPLAY TEMP-IN "° " LABEL-IN " is " TEMP-OUT "° " LABEL-OUT.
Program Notes
[edit | edit source]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
[edit | edit source]Copy and paste the code above into one of the following free online development environments or use your own Conditions compiler / interpreter / IDE.
Assignment
[edit | edit source]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
with x representing an unknown, a, b and c representing constants with a ≠ 0, the quadratic formula is:
where the plus-minus symbol "±" indicates that the quadratic equation has two solutions. Written separately, they become:
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 = ax2 + 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.