Showing posts with label #cobolprogramming. Show all posts
Showing posts with label #cobolprogramming. Show all posts

Thursday, 4 September 2014

COBOL Multiply Statement: Syntax, Examples, and Best Practices! #cobol


COBOL Multiply Statement
COBOL Multiply Statement

In COBOL programming, the Multiply Statement holds significant importance, offering a powerful capability to perform arithmetic operations efficiently. This article dives into the intricacies of the COBOL Multiply Statement, exploring its syntax, functionality, examples, advantages, common errors, and best practices. This article talks about the following key points about COBOL Multiply Statement:

Agenda.
  • Introduction to COBOL Multiply Statement.
  • Syntax of COBOL Multiply Statement.
  • Explanation of COBOL Multiply Statement.
    • Multiplying Two Numbers.
    • Multiplying by a Constant.
    • Multiplying by a Variable.
  • Examples of COBOL Multiply Statement.
  • Advantages of Using COBOL Multiply Statement.
  • Common Errors and Troubleshooting.
  • Best Practices for Using COBOL Multiply Statement.
  • Conclusion.


Introduction to COBOL Multiply Statement

COBOL, for Common Business-Oriented Language, is renowned for its robustness in processing business data. The Multiply Statement in COBOL is a fundamental element for performing multiplication operations within programs. It allows programmers to multiply numeric data items, whether constants, variables, or expressions, providing flexibility in computation.

Syntax of COBOL Multiply Statement.

The MULTIPLY statement multiplies numeric items and sets the values of data items equal to the results. Multiply arithmetic statements have two different formats. 
         MULTIPLY operand-1 BY operand-2 GIVING result.
Here, operand-1 and operand-2 represent the values to be multiplied, and the result denotes the variable to store the outcome of the multiplication.
Here is a quick glimpse of the syntax:
 Format 1: Multiply Statement Syntax.

Multiply statement in COBOL
COBOL Multiply Verb.

Here, the value of identifier-1 or literal-1 is multiplied by the value of identifier-2; the product is then placed in identifier-2. For each successive occurrence of identifier-2, the multiplication occurs in the left-to-right order in which identifier-2 is specified.

Format 2: Multiply Statement Syntax. 

COBOL Multiply Statement syntax
COBOL Multiply Verb Syntax. 

Here, the value of identifier-1 or literal-1 is multiplied by the value of identifier-2 or literal-2. The product is then stored in the data item(s) referenced by identifier-3.

Explanation of COBOL Multiply Statement.

Multiplying Two Numbers.

The primary usage of the COBOL Multiply Statement involves multiplying two numeric values together. This is achieved by specifying the operands to be multiplied using the BY keyword.

Multiplying by a Constant.

In some scenarios, it's necessary to multiply a numeric variable by a constant value. COBOL facilitates this by using constants directly within the Multiply Statement.

Multiplying by a Variable.

Another common scenario is multiplying a variable by another variable. COBOL enables this operation by specifying the variables as operands in the Multiply Statement.
Refer to the COBOL Multiply example below for more details.

Multiply Example.
COBOL Multiply Example 

Advantages of Using COBOL Multiply Statement.

The COBOL Multiply Statement offers several advantages, including:
  • Efficient computation of multiplication operations.
  • Simplified syntax for performing multiplication.
  • Flexibility in handling various data types.

Common Errors and Troubleshooting.

While using the COBOL Multiply Statement, programmers may encounter errors such as:
  • Data type mismatch between operands.
  • Overflow or underflow issues during computation.
  • Incorrect usage of the GIVING clause.
To troubleshoot these errors, thorough testing and debugging procedures are recommended.

Best Practices for Using COBOL Multiply Statement.

To ensure optimal utilization of the COBOL Multiply Statement, programmers should adhere to best practices such as:
  • Proper data validation to prevent unexpected results.
  • Clear and descriptive naming conventions for operands and result variables.
  • Regular code reviews to identify and rectify potential issues.

Conclusion.

In conclusion, the COBOL Multiply Statement is a vital component of COBOL programming, offering a convenient way to perform multiplication operations. Programmers can leverage this statement effectively in their COBOL programs by understanding its syntax, functionality, examples, advantages, common errors, and best practices.

FAQs (Frequently Asked Questions).

What is the purpose of the COBOL Multiply Statement?

The COBOL Multiply Statement performs multiplication operations within COBOL programs, allowing efficient computation of numeric values.

Can the COBOL Multiply Statement handle the multiplication of variables?

Yes, the COBOL Multiply Statement can multiply variables together, providing flexibility in arithmetic operations.

What are some common errors encountered while using the COBOL Multiply Statement?

Common errors include data type mismatch, overflow/underflow issues, and incorrect usage of the GIVING clause.

Are there any best practices for using the COBOL Multiply Statement?

Yes, best practices include proper data validation, clear naming conventions, and regular code reviews to ensure optimal usage.

How does the COBOL Multiply Statement contribute to efficient computation?

The COBOL Multiply Statement simplifies the process of performing multiplication operations, enhancing the efficiency of COBOL programs.


Subscribe to Topictrick, & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 

Mainframe Forum™


Thursday, 14 August 2014

COBOL Subtract Statement | SUBTRACT STATEMENT Example.


Numerical calculation is one of the most critical part of programming language. In-fact most of the programming language provide various functions that can be used to implement business calculation logic. 

Similarly, COBOL also provide various clause such as SUBTRACT, MULTIPLY, ADD, DIVIDE, COMPUTE etc to perform various calculation

In this tutorial, I would focus on various format of SUBTRACT clause available in COBOL. I would try to explain syntax with few examples.

The SUBTRACT statement subtracts one numeric item, or the sum of two or more numeric items, from one or more numeric items and stores the result. Subtract has three different format which is applicable in various situation. 

Format : 1
Subtract
COBOL Subtract From.
Example: 

Add a note hereAdd a note hereSUBTRACT 1 FROM WS-COUNTER.

Here, all identifiers or literals preceding the key word FROM are added together and this sum is subtracted from and stored immediately in identifier-2. This process is repeated for each successive occurrence of identifier-2, in the left -to-right order in which identifier-2 is specified.
Format : 2
Subtract in COBOL
COBOL Subtract From Giving
Example:
SUBTRACT WS-HOURS FROM TOT-HOURS GIVING OVERTIME-HRS. 
Here, all identifiers or literals preceding the key word FROM are added together and this sum is subtracted from identifier-2 or literal-2. The result of the subtraction is stored as the new value of each data item referenced by identifier-3.
Format : 3
COBOL Tutorial
Subtract From in COBOL
Example:

SUBTRACT WS-AMT1 WS-AMT2 WS-AMT3 FROM GRS-PAY GIVING NET-PAY.
Here, the elementary data items within identifier-1 are subtracted and stored in the corresponding elementary items within identifier-2.

COBOL TUTORIAL

Check-out my tutorial on COBOL BLANK WHEN ZERO Clause.

COBOL REDEFINE Clause | REDEFINE in COBOL Example and rules.

COBOL
COBOL REDEFINE Tutorial
Another important statement/feature of COBOL in REDEFINES clause. The Redefines clause lets you define a field in storage in two or more ways. 

When used, this clause must have the same level number as the data item it is redefining and it must come right after that item.

The REDEFINES clause allows you to use different data description entries to describe the same computer storage area.   

Always remember, the definition and the redefinition both refer to the same bytes of storage, you can code a Value clause only on the original definition. 
Syntax : 
01-49  data-name-1 REDEFINES data-name-2

Example 1: A Redefines clause that redefines a date field

Add a note hereAdd a note here05  WS-TDY-DTE       PIC 9(08).
05  WS-TDY-DTE-X     REDEFINES WS-TDY-DTE.
    10  TDY-YR       PIC 9(04).
    10  TDY-MNTH     PIC 9(02).
    10  TDY-DAY      PIC 9(02).

Example 2 : A Redefines clause that redefines a print field

Add a note hereAdd a note here05  WS-CHG-PERNT     PIC ZZ9.9-.
05  WS-CHG-PERNT-X   REDEFINES WS-CHG-PERNT
                     PIC X(6).

Example 3 : A Redefines clause that redefines alphanumeric with numeric.

     05  WS-EMP-REC       PIC X(20).
  05  WS-SAL-AMT       REDEFINES     WS-EMP-REC.
      10 WS-EMP-SALE   PIC 9(05)v99. 
      10 WS-EMP-TAX    PIC 9(02)v99. 

Rule : 
  1. Level number, data-name-1, and FILLER are not part of the REDEFINES clause itself and are included in the format only for clarity.
  2. Level-numbers of data-name-1 and data-name-2 must be identical and must not be level 66 or level 88.
  3. Data-name-1, FILLER identifies an alternate description for the same area and is the redefining item or the REDEFINES subject.
  4. Data description entry for the redefined item cannot contain an OCCURS clause.
  5. More than one redefinition of the same storage area is permitted. The entries giving the new descriptions of the storage area must immediately follow the description of the redefined area without intervening entries that define new character positions.
  6. Multiple redefinitions must all use the data-name of the original entry that defined this storage area.
  7. Redefining entry (identified by data-name-1) and any subordinate entries, must not contain any VALUE clauses.

START CLAUSE EXAMPLE

Have a look of COBOL Start Statement?



COBOL Rounded Clause | ROUNDED in COBOL Example [COBOL ROUNDED Statement].

COBOL ROUNDED Phrase, COBOL Rounding

COBOL ROUNDED Phrase

Welcome back to today's session on "COBOL Rounded Clause" or "COBOL Rounding". In this session, you'll learn the basics of the COBOL ROUNDED statement and why you use COBOL rounding in your application programs.  You'll go through the COBOL rounding up an example for better understanding. Let's get started with the COBOL Rounded Phrase introduction.  
Agenda. 
  • Introduction. 
  • What is COBOL ROUNDED Clause (i.e. COBOL ROUNDED Statement)?
  • COBOL ROUNDED Syntax. 
  • COBOL ROUNDED Example. 
    • ROUNDING in COBOL Example.
  • ROUNDED in COBOL Rules.
  • Conclusion.

Introduction. 

The capability of rounding up or rounding down is another important feature of any programming language. There are many business scenarios where monetary transaction value is either round-up or rounded down based on the requirement. COBOL ROUNDED Phrase (i.e. COBOL ROUNDED statement) is used to implement rounding in COBOL programs. In COBOL, you do not have any default rounding. You've to specify the rounding in your program. 
 

COBOL Rounding Rule of Thumb. 

  • If the ending value is 5 or more then round Up (i.e. COBOL round up).
  • If the ending value is 4 or less then round Down (i.e. COBOL round down).

What is COBOL ROUNDED Clause (i.e. COBOL ROUNDED Statement)?


COBOL ROUNDED Clause, ROUNDED in COBOL

COBOL ROUNDED CLAUSE
Once the decimal point is aligned, the number of places given for the fraction of the resultant identifier is compared to the number of places in the fraction of the product of an arithmetic operation. 

Unless the COBOL ROUNDED phrase is known, truncation occurs when the size of the fractional result exceeds the number of places given for its storage.

When the COBOL ROUNDED verb is used, the resulting identifier's least significant digit is increased by 1 if the excess's most significant digit is greater than or equal to 5.

COBOL Rounding or truncation occurs relative to the rightmost integer position for which storage is allocated when the resultant identifier is defined by a PICTURE clause containing the rightmost Ps and the number of places in the measured result exceeds the number of integer positions stated.

Important: The COBOL ROUNDED phrase has no effect in a floating-point arithmetic operation; the output of a floating-point operation is always rounded.

COBOL ROUNDED Syntax. 

The syntax of ROUNDED in COBOL is pretty simple and straightforward. You need to specify the ROUNDED keyword as illustrated in the following figure.  
COBOL ROUNDED, ROUNDED in COBOL
ROUNDED  in COBOL Example.

When the result is truncated on either the left or right side after decimal point alignment, this is known as a size error message. Let's look at the following COBOL round down and COBOL rounds up examples.

ROUNDED in COBOL Example. 

Destination Field   |  Actual Result   | Truncated Result | Rounded Result
PIC 9(03)v9.     123.26         123.2          123.3
PIC 9(03)v9.     234.54         234.5          234.5 
PIC 9(03).       456.25         456            456 

Important Point: Division by 0 always leads to SIZE ERROR. 

ROUNDED in COBOL Example: 

COMPUTE SALES-TAX ROUNDED = SALES-AMOUNT * .0785


Rounded in COBOL Rules:

  1. ROUNDED in COBOL can be used with any arithmetic operation. 
  2. Data will be truncated if you did not specify COBOL Rounded Clause and the resultant fields is not sufficient to store the decimal data. 
  3. When the COBOL ROUNDED phrase is used, the output will be rounded to the PICTURE specification of the destination field.
  4. When ROUNDED in COBOL and REMAINDER are used in the same operation then DIVIDE, ROUNDED must be defined first.
You can code the Rounded clause whenever the result of a calculation can have more decimal places than is specified in the picture of the result field. If you don’t use the Rounded clause, the extra decimal places are truncated

Conclusion.

Finally, this marks an end to our today's COBOL ROUNDED clause. In this session, you learn what is COBOL rounding or COBOL ROUNDING Function. You learned COBOL ROUNDED Statement syntax along with COBOL round-up and COBOL round-down examples for better understanding.  


COBOL Tutorial

Have a look at CICS Top 100 Interview Questions Part II.


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™


Monday, 11 August 2014

COBOL START Statement | COBOL START Position Example | START statement in COBOL [COBOL start key is greater].

COBOL Start statement, START statement in COBOL

COBOL START Statement

Welcome back to today's session on "COBOL START Statement". In this session, you'll learn the basics of START statements in COBOL, followed by COBOL START statement syntax and COBOL Start statement example for better understanding. You'll also learn how to use the COBOL start key is greater than in your COBOL program. Let's get started with an introduction to the COBOL START statement.

COBOL START Statement - Agenda.

  • Introduction.
  • What is a COBOL START Statement.
  • COBOL START Syntax. 
  • How COBOL START Statement works.
  • COBOL START Example.
  • Conclusion.

Introduction.

File handling is one of the important aspects of any programming language. In COBOL you have many statements that can be used to read, write or update data into a sequential file, VSAM file, or DB2 database. COBOL START statement is one such statement that provides more control over the process to read data from the file. 

What is a COBOL START Statement?

In laymen's terms, the START statement in COBOL is used to position the pointer within an indexed or relative file to retrieve a record from the file. You must open the associated indexed or relative file in an appropriate mode (i.e. Input or I-O mode) before you issue the COBOL START command. 
COBOL Tutorial

COBOL START KEY Tutorial

The COBOL START statement enables the programmer to position the relative file at some specified point so that subsequent sequential operations on the file can start from this point instead of the beginning. 
The KEY IS phrase in COBOL START indicates how the file is to be positioned. 
The data-name in this phrase must be the data-name in the RELATIVE KEY phrase of the SELECT . . . ASSIGN . . . clause. 

When the EQUAL TO or NOT LESS THAN condition is specified, the file is positioned at the point indicated by the relative key-data item. 

When the GREATER THAN condition is specified, the file is positioned at the next relative position of the position indicated by the RELATIVE KEY data item.

COBOL START Statement Syntax.

The COBOL START statement is pretty simple and easy to understand. But, you have to be careful while using the COBOL START statement in your programs. You must clearly understand the requirement before implementing the COBOL START VERB. Let's focus on the following START statement syntax. 


COBOL Tutorial, COBOL Start statement in COBOL, COOBL START

START STATEMENT in COBOL.

  • FILE Name: You must specify the file name after the START keyword. The file details must be defined correctly in your COBOL program. 
  • KEY Phrase: When the KEY phrase is specified, the file position indicator is positioned at the logical record in the file whose key field satisfies the comparison. If the KEY phrase is not defined, KEY IS EQUAL (to the prime record key) is implied.
  • INVALID KEY: The invalid key function will be trigger if there is no record that satisfies comparison criteria. 
  • END-START phrase: is the explicit scope terminator for the COBOL START statement.

How COBOL START Statement works?

In nutshell, the START statement in COBOL is used to set a pointer before you issue a COBOL READ command to read the record from the file.  As a thumb rule, before you issue a Start statement, you must place a key value in the RECORD KEY field. Then, the Key clause of the Start statement establishes the first record to be processed. As you can see, that record can have a value that’s either equal to, greater than (i.e.  COBOL start key is greater), or equal to the value you specify. 
  

COBOL START Example. 

In the following example, 1000 is moved to item-no. The item-no is used as a primary key to read the data from the file. 
COBOL Tutorial, COBOL START Example, START Statement with COBOL

COBOL START STATEMENT EXAMPLE.

#1. COBOL Startment Statement Example: 

 IDENTIFICATION DIVISION.
*
   ....
   ....
*
 PROCEDURE DIVISION.
*
 A00-MAIN-MODULE.
     PERFORM D00-OPEN-FILE 
     PERFORM E00-PREP-KEY
     PERFORM F00-READ-FILE
     ....
     ....
     ....
  STOP RUN.
*
 D00-OPEN-FILE. 
     OPEN INPUT EMPMST.
 E00-PREP-KEY. 
     MOVE 1090     TO EMP-IND
     START EMPMST KEY IS => EMP-ID
           INVALID KEY "Input Valid Key."
     END-START. 
*
 F00-PREP-KEY.
     MOVE SPACES   TO EMP-REC
     READ EMPMST   
          AT END 'END OF FILE'
     END-READ.  
 .....
 .....
 ..... 
 

Conclusion. 

Finally, this marks an end to the COBOL START Statement. In this session, you learn the basics of the COBOL START statement. You also learn different parameters of COBOL START statement syntax followed by the COBOL Start statement example for better understanding. You also learned how to use the COBOL start key is greater than in your COBOL program.  Do check out COBOL LEVEL 88 Condition.


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™

New In-feed ads