COBOL400 – Introduction
Introduction
COBOL400: This is the COBOL programming language used on IBM’s AS/400 (now IBM i) platform. It is a foundational, rules-based language that has been used for decades.
ILE COBOL: This is the Integrated Language Environment version of COBOL for the IBM i. It is a more modern form of COBOL that works with other languages like RPG and C++ in one environment.
Source Types in COBOL400 or ILE COBOL
There are 4 types of source types available for COBOL programs in IBM i-Series
- CBL
- CBLLE
- SQLCBL
- SQLCLBLLE
Source Types brief description
Each source type of COBOL contains different features as shown below:
- CBL – Native COBOL + Features available in IBM i-Series
- CBLLE – Native COBOL + Features available in IBM i-Series + ILE features of IBM i-Series
- SQLCBL – Native COBOL + Features available in IBM i-Series + Embedded DB2 programming
- SQLCBLLE – Native COBOL + Features available in IBM i-Series + Embedded DB2 programming + ILE features of IBM i-Series
COBOL Program objects
-
Compilation of CBL Source using the CRTCBLPGM CL Command.
When CBL source is compiled, it generates program object – *PGM with attribute CBL.
-
Compilation of CBLLE Source using the CRTBNDCBL CL Command
When CBLLE source is compiled, it generates program object – *PGM with attribute CBLLE.
-
Compilation of SQLCBL Source using the CRTSQLCBL CL Command.
When SQLCBL source is compiled, it generates program object – *PGM with attribute CBL.
-
Compilation of SQLCBLLE Source using the CRTSQLCBLI CL Command.
When SQLCBLLE source is compiled, it generates program object – *PGM with attribute CBLLE
Looping Statements
Perform Looping Statements
Iterative programming uses loops to make a set of statements run repeatedly. The statements keep executing until a specified condition is met.
In COBOL, the PERFORM statement is used for this kind of repetitive execution.
PERFORM Types based on lines
There are 2 types of the PERFORM statements based on lines:
- Inline PERFORM
- Outline PERFORM
PERFORM Types based on formats
There are four types of PERFORM statements based on formats:
Format 1 – Basic PERFORM
Simple PERFORM is a way to execute a given paragraph or section once, and control is passed to the next statement in the flow. It is both inline and outline.
- Example1 for Inline PERFORM:
Output:
- Example 2 for Outline PERFORM:
Output:
- Example for Outline PERFORM with THRU:
Output:
Format 2 – PERFORM with TIMES Phrase
- Example for Inline PERFORM with TIMES:
Output:
- Example for Outline PERFORM with TIMES:
Output:
Format 3 – PERFORM with UNTIL Phrase
- Example for Inline PERFORM with UNTIL:
Output:
- Example for Outline PERFORM with UNTIL:
Output:
Format 4 – PERFORM with VARYING Phrase
PERFORM with VARYING executes a specific paragraph or section repetitively while varying the values of the variables until the condition is satisfied. The PERFORM with VARYING is similar to PERFORM UNTIL, but it handles the variable’s initialization, increment, or decrement. This structure provides a “for loop” functionality found in many other programming languages.
- Example for Inline PERFORM with VARYING:
Output:
- Example for Outline PERFORM with VARYING:
Output:
String Handling
String Handling
String handling commands are essential tools for working with and changing text (like characters and strings) in a program. It performs various operations, such as combining or separating strings, and finding or working with specific parts of a string. Programmers can also use them to count, replace, or convert characters. For example, changing text to uppercase or lowercase, or checking its total length.
In COBOL programming, three main commands handle these tasks:
- STRING
- UNSTRING
- INSPECT
STRING Verb
The STRING statement strings together the partial or complete contents of two or more data items into one single data item. Also, single STRING statement can be written instead of a series of MOVE statements.
Syntax:
Example 1:
Output:
Example 2:
Output:
Example 3:
Output:
UNSTRING Verb
The UNSTRING statement causes contiguous data in a sending field to be separated and placed into multiple receiving fields. Also, single UNSTRING statement can be written instead of a series of MOVE statements.
Syntax:
Example 1:
Output:
Example 2:
Output:
Example 3:
Output:
Example 4:
Output:
INSPECT Verb
The INSPECT statement specifies that characters in a data item are to be counted (tallied), or replaced (or both).
- It will count the occurrence of a specific character (alphabetic, numeric, or special character) in a data item.
- It will fill all or portions of a data item with spaces or zeros.
- It will translate characters from one collating sequence to another.
The INSPECT statement has four formats:
- INSPECT TALLYING
- INSPECT REPLACING
- INSPECT TALLYING REPLACING
- INSPECT CONVERTING
Syntax for Format 1: INSPECT TALLYING
Example 1:
Output:
Syntax for Format 2: INSPECT REPLACING
Example 1:
Output:
Syntax for Format 3: INSPECT TALLYING REPLACING
Example 1:
Output:
Syntax for Format 4: INSPECT CONVERTING
Example 1:
Output: