مرکزی مواد پر جائیں
eLearner.app
ماڈیول 2 · سبق 1 از 2کورس میں 3/10~12 min
ماڈیول اسباق (1/2)

COBOL ریاضی

In COBOL, mathematical operations can be performed either using dedicated verbs (ADD, SUBTRACT, MULTIPLY, DIVIDE) or using the generic and expressive COMPUTE statement.

Arithmetic Operations with Formal Verbs

COBOL provides specific textual instructions for each basic arithmetic operation:

1. Addition (ADD)

Adds one or more values to a variable.

Code
ADD WS-NUM-A TO WS-NUM-B.
* Equivalent to: WS-NUM-B = WS-NUM-B + WS-NUM-A

If you want to save the result in a third variable without modifying the addends, use the GIVING clause:

Code
ADD WS-NUM-A TO WS-NUM-B GIVING WS-RESULT.

2. Subtraction (SUBTRACT)

Subtracts one value from another.

Code
SUBTRACT 10 FROM WS-BALANCE.
SUBTRACT WS-TAX FROM WS-SUBTOTAL GIVING WS-TOTAL.

3. Multiplication (MULTIPLY) and Division (DIVIDE)

Multiply and divide values in a similar fashion.

Code
MULTIPLY WS-QTY BY WS-PRICE GIVING WS-TOTAL.
DIVIDE WS-TOTAL BY WS-ITEMS GIVING WS-AVERAGE.

The COMPUTE Statement

For complex mathematical expressions, using individual arithmetic verbs can be long and difficult to read. The COMPUTE statement allows you to use standard mathematical operators (+, -, *, /, ** for exponentiation) similar to modern languages.

Code
COMPUTE WS-TOTAL = (WS-PRICE * WS-QUANTITY) - WS-DISCOUNT.

Try it yourself

ورزش#cobol.m2.l1.e1
کوششیں: 0لوڈ ہو رہا ہے…

Write a statement inside the PROCEDURE DIVISION using the ADD verb to sum WS-NUM-A to WS-NUM-B and store the result in WS-RESULT.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Use the syntax: ADD WS-NUM-A TO WS-NUM-B GIVING WS-RESULT.

3 کوششوں کے بعد حل دستیاب ہے۔

ورزش#cobol.m2.l1.e2
کوششیں: 0لوڈ ہو رہا ہے…

Use the COMPUTE statement to calculate the value of WS-RESULT applying the formula: two times WS-NUM-A minus WS-NUM-B.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Use spaces around the * and - operators: COMPUTE WS-RESULT = (WS-NUM-A * 2) - WS-NUM-B.

3 کوششوں کے بعد حل دستیاب ہے۔