0% found this document useful (0 votes)
232 views19 pages

Introduction To NATURAL PDF

This document provides an introduction to the NATURAL programming language. It describes what NATURAL is, the different types of NATURAL data areas including local, global, and parameter data areas. It also defines NATURAL variable types and how to define variables and data areas. Finally, it gives examples of common NATURAL statements including READ, FIND, STORE, UPDATE, and DELETE.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views19 pages

Introduction To NATURAL PDF

This document provides an introduction to the NATURAL programming language. It describes what NATURAL is, the different types of NATURAL data areas including local, global, and parameter data areas. It also defines NATURAL variable types and how to define variables and data areas. Finally, it gives examples of common NATURAL statements including READ, FIND, STORE, UPDATE, and DELETE.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Introduction to NATURAL

02/07/2009
This document gives a basic introduction to NATURAL programming. Details what is NATURAL, NATURAL Data areas, NATURAL Variables and Statements.

Insurance / ISU 01 Lise Molineus Jaicy .P [email protected]

Introduction to NATURAL

What is NATURAL?
NATURAL is a 4th generation programming language. It is free format language where it can be either procedural or non-procedural. Can be used to build both online and batch programs. Databases like ADABAS and DB2 can talk to NATURAL. There are two modes of NATURAL programming, Structured and Reporting. The functional differences between structured mode and reporting mode are provided at the end of the document.

Defining Data in NATURAL


Types of Variables and Data Areas Defining Database Views Defining User Variables Redefinition Of Variables

Types of Variables:
Type Alphanumeric Numeric Packed decimal Control Logical Date Time Floating Point Integer Binary Format (Annn) (Nnn.m) (Pnn.m) (C) (L) (D) (T) (Fn) (In) (B) Size 1-253 characters 1-29 digits 1-29 digits 2-byte internal format 1-byte FALSE/TRUE P6 P12 4 bytes for single precision 8 bytes for double precision or 1,2 or 4 bytes 1-128 bytes

Internal Use

Introduction to NATURAL

Example: DEFINE DATA LOCAL 01 #NAME (A25)

01 #EMP-ID (N10) END-DEFINE

DATA Areas:
The data areas are used to maintain external NATURAL Data Definitions. Following are the three data areas.

LDA (Local Data Area) Data resides within the program and is used in the program itself. GDA (Global Data Area) Data resides in a common location and is shared by all Natural objects in the application

PDA (Parameter Data Area) Data passed between a natural program and its subprogram, subroutine or a help routine.

Local Data Area


Variables defined as local can be used only within a single NATURAL module. There are two options for defining local data: Defining the data within the program. Defining the data in a local data area outside the program.

In the below example, the fields are defined within the DEFINE DATA statement of the program. In the second example, the same fields are defined in a local data area, and the DEFINE DATA statement only contains a reference to that data area.

Internal Use

Introduction to NATURAL

Example 1 - Fields Defined within a DEFINE DATA Statement:

DEFINE DATA LOCAL 01 #EMP-ID 01 #EMP-NAME 01 #EMP-ADDR 01 #EMP-CITY 01 #EMP-SAL END-DEFINE Example 2 - Fields Defined in a local data area outside the program. (N20) (A25) (A20) (A25) (P9.2)

Sample LDA: 01 #INPUT-FILE R 01 #INPUT-FILE 02 #GRP-NUM 02 #EMP-NO 02 #EMP-NAME N 9.0 N 9.0 A 10 A 28

Program reference: DEFINE DATA LOCAL USING SAMPLDA END-DEFINE

Global Data Area


The data elements that are to be used by more than one program, routine should be defined in a global data area. Variables defined in a global data area may be referenced by several objects in an application. The global data area and the objects which reference it must be in the same library. Global data areas must be defined with the data area editor, and a program using that data area must reference it in the

Internal Use

Introduction to NATURAL

DEFINE DATA statement. Any number of main programs, external subroutines and help routines can share the same global data area.

Each object can reference only one global data area; that is, a DEFINE DATA statement must not contain more than one GLOBAL clause. Example: DEFINE DATA GLOBAL USING SAMPLGDA END-DEFINE

Parameter Data Area


Parameter data areas are used by subprograms and external subroutines. A subprogram is invoked with a CALLNAT statement. With the CALLNAT statement, parameters can be passed from the invoking object to the subprogram. These parameters must be defined with a DEFINE DATA PARAMETER statement in the subprogram: they can be defined in the PARAMETER clause of the DEFINE DATA statement itself; or they can be defined in a separate parameter data area, with the DEFINE DATA PARAMETER statement referencing that parameter data area.

The sequence, format and length of the parameters specified with the CALLNAT/ PERFORM statement in the invoking object must exactly match the sequence, format and length of the fields specified in the DEFINE DATA PARAMETER statement of the invoked subprogram/subroutine. However, the names of the variables in the invoking object and the invoked subprogram/subroutine need not be the same (as the parameter data are transferred by address, not by name). Sample PDA: 01 # GRP-DET R 01 # GRP-DET 02 #GRP-NUM 02 #EMP-NO 02 #EMP-NAME Program reference: DEFINE DATA PARAMETER USING SAMPLPDA END-DEFINE N 9.0 N 9.0 A 10 A 28

Internal Use

Introduction to NATURAL

Defining Database Views


View consists of database fields to be referenced. It can be same as defined in DDM May define own subset of DDM May use same database fields in different view May be defined internal or external (using data areas). SYNTAX: View-name VIEW [OF] DDM-name { level {ddm-field [(format length)] } Example: 1 FX-BOOK VIEW OF GTI-FX-BOOK 2 TRAN-TYPE 2 GTD-RECEIPT-IND 2 REDEFINE GTD-RECEIPT-IND 3 #RECEIPT-IND 2 SOURCE-SYSTEM 2 C*CHANGE-HISTORY-GROUP 2 CHANGE-HISTORY-GROUP 3 PREVIOUS-CHANGE-USER 3 PREVIOUS-CHANGE-DATE (1:99) (A1)

Defining User Variables


Variables can be defined and used when required and now that we know the data types the variables can fit in, lets see how to use them. For Example: DEFINE DATA LOCAL 01 #NAME 01 #EMP-ID (A25) (N10)

01 #EMP-SAL (N10.2) END-DEFINE The variables should be declared within the DEFINE DATA and END-DEFINE section. The variables can be used only when declared. NATURAL throws a syntax error if variables are used
Internal Use

Introduction to NATURAL

without declaring them initially. The layouts of input file / output file and other working storage variables can be declared and used. In the above example, the variable #NAME is alphanumeric and of size 25, which means that it can hold names that have 25 characters or less.

Re-defining User Variables


Now that we know how to define variables, and if you observe closely they are not just defined, but defined in levels. Weve seen examples where the variables are declared in 01 level. A variable can be redefined and can have sublevels. Let us take the same example and try redefining the 01 level variable.

DEFINE DATA LOCAL 01 #NAME (A25)

01 REDEFINE #NAME 02 # FORE-NAME (A10) 02 # SURNAME 01 #EMP-ID (N10) (A15)

01 #EMP-SAL (N10.2) END-DEFINE

The name comprises of two parts, forename and surname. The above redefinition shows that the forename is alphanumeric and of size 10 and surname is alphanumeric and of size 15 and both adds up to 25 characters.

NATURAL Statements
Lets have a look at some of the most frequently used NATURAL Statements in detail. ACCEPT/REJECT Syntax: ACCEPT IF logical-condition REJECT IF logical-condition Example: READ EMPLOY-VIEW ACCEPT IF #SEX = 'F' AND #MAR-STAT = 'S' DISPLAY #NAME END-READ
Internal Use

Introduction to NATURAL

ADD Syntax: ADD ROUNDED operand1 ... TO operand2 ADD ROUNDED operand1 ... GIVING operand2 Example: ADD +5 -2 -1 GIVING #A ADD .231 3.6 GIVING #B ADD ROUNDED 2.9 3.8 GIVING #C SUBTRACT Syntax: SUBTRACT ROUNDED operand1 ... FROM operand2 SUBTRACT ROUNDED operand1 ... FROM operand2 GIVING operand3 Example: DEFINE DATA LOCAL 1 #A (P2) INIT <50> 1 #B (P2) 1 #C (P1.1) INIT <2.4> END-DEFINE SUBTRACT 6 FROM #A SUBTRACT 6 FROM 11 GIVING #A SUBTRACT 3 4 FROM #A GIVING #B SUBTRACT -3 -4 FROM #A GIVING #B SUBTRACT ROUNDED 2.06 FROM #C MULTIPLY Syntax: MULTIPLY ROUNDED operand1 BY operand2 MULTIPLY ROUNDED operand1 BY operand2 GIVING operand3 Example: MULTIPLY #A BY 3 GIVING #C MULTIPLY ROUNDED 3 BY 3.5 GIVING #C #A: 44 #A: 5 #A: 5 #B: -2 #A: 5 #B: 12 #C: 0.3 #A: 2 #B: 3.8 #C: 7

Internal Use

Introduction to NATURAL

DIVIDE Syntax: DIVIDE ROUNDED operand1 INTO operand2 GIVING operand3 DIVIDE operand1 INTO operand2 GIVING operand3 REMAINDER operand4 Example: DEFINE DATA LOCAL 1 #A (N7) INIT <20> 1 #B (N7) 1 #C (N3.2) 1 #D (N1) 1 #E (N1) INIT <3> 1 #F (N1) END-DEFINE

DIVIDE 5 INTO #A GIVING #B DIVIDE 3 INTO 3.1 GIVING #D DIVIDE 2 INTO #E REMAINDER #F

#B: 4 #D: 1 #E: 1 #F: 1

READ Syntax: READ operand1 RECORDS IN FILE view-name sequence/range-specification STARTING WITH ISN=operand4 WHERE-clause statement ... END-READ

Example: READ EMPLOY-VIEW BY ISN STARTING FROM 1 ENDING AT 3 DISPLAY PERSONNEL-ID NAME *ISN *COUNTER END-READ

Internal Use

Introduction to NATURAL

READ EMPLOY-VIEW IN PHYSICAL SEQUENCE DISPLAY PERSONNEL-ID NAME *ISN *COUNTER END-READ

FIND Syntax: FIND |option| RECORDS IN FILE view-name WITH-clause STARTING WITH ISN=operand5 END-FIND Example: FIND EMPLOY-VIEW WITH CITY = `FRANKFURT` DISPLAY FIRST-NAME PERSONNEL-ID END-FIND

STORE Syntax: STORE RECORD IN FILE view-name WITH FIELD-NAME = Value END-TRANSACTION

Example: STORE RECORD IN EMPLOYEES WITH PERSONNEL-ID = #PERSONNEL-ID NAME = #NAME #FIRST-NAME #MAR-STAT #BIRTH #CITY

FIRST-NAME = MAR-STAT BIRTH CITY END TRANSACTION = = =

WRITE NOTITLE `RECORD HAS BEEN ADDED`

Internal Use

10

Introduction to NATURAL

UPDATE Syntax: UPDATE RECORD IN STATEMENT (r) Example: FIND EMPLOY-VIEW WITH NAME = #NAME MOVE CHENNAI to #CITY UPDATE END TRANSACTION END-FIND

DELETE Syntax: DELETE RECORD IN STATEMENT (r) ) Example: FIND EMPLOY-VIEW WITH NAME = #NAME IF EMP-ID EQ 52103 DELETE END TRANSACTION END-IF END-FIND

END TRANSACTION Syntax: END OF TRANSACTION operand1 ... Example: FIND EMPLOY-VIEW WITH CITY = `BOSTON` ASSIGN COUNTRY = `USA` UPDATE END TRANSACTION END-FIND

Internal Use

11

Introduction to NATURAL

READ WORK FILE x Syntax: READ WORK FILE work-file-number ONCE RECORD operand1 statement... statement... statement... END-WORK

Example: READ WORK FILE 1 RECORD #RECORD DISPLAY NOTITLE #PERS-ID #NAME END-WORK

CLOSE WORK FILE Syntax: CLOSE WORK FILE work-file-number Example: READ WORK FILE 1 ONCE #W-DAT AT END OF FILE WRITE '*** END OF FILE ***' END-ENDFILE WRITE #W-DAT INPUT 'FROM START (Y/N) ?' #PRMPT IF #PRMPT NE 'Y' THEN STOP END-IF CLOSE WORK FILE 1

WRITE WORK FILE Syntax: WRITE WORK FILE work-file-number VARIABLE operand1 ...

Internal Use

12

Introduction to NATURAL

Example: FIND EMPLOY-VIEW WITH CITY = `LONDON` WRITE WORK FILE 1 PERSONNEL-ID NAME END-FIND CALLNAT Syntax: CALLNAT operand1 USING operand2 Example: /* MAIN PROGRAM `MAINP1` DEFINE DATA LOCAL 1 #FIELD1 (N6) 1 #FIELD2 (A20) 1 #FIELD3 (A10) CALLNAT `SUBP1` #FIELD1 (AD=M) #FIELD2 (AD=O) #FIELD3 `P4 TEXT`

/* SUBPROGRAM `SUBP1` DEFINE DATA PARAMETER 1 #FIELDA (N6) 1 #FIELDB (A20) 1 #FIELDC (A10) END-DEFINE

DECIDE ON Syntax: DECIDE ON FIRST VALUES OF operand1 EVERY VALUES operand2 ,operand2... :operand2 statement... ... ANY VALUES statement... ALL VALUES statement... NONE VALUES statement... IGNORE END-DECIDE

Internal Use

13

Introduction to NATURAL

Example: DECIDE ON FIRST VALUE OF *PF-KEY VALUE `PF1` PERFORM ROUTINE-UPD VALUE `PF2` PERFORM ROUTINE-ADD ANY VALUE END TRANSACTION WRITE `RECORD HAS BEEN MODIFIED` NONE VALUE IGNORE END-DECIDE END Syntax: END Example: FIND ... READ ... END-READ END-FIND END ESCAPE Syntax: ESCAPE TOP BOTTOM (r) IMMEDIATE ROUTINE IMMEDIATE Example: FND. FIND EMPLOY-VIEW WITH CITY = #CITY IF NO RECORDS FOUND WRITE `NO RECORDS FOUND` ESCAPE BOTTOM (FND.) END-NOREC

Internal Use

14

Introduction to NATURAL

IF #CNTL = 'D' ESCAPE BOTTOM (FND.) END-IF END-FIND FOR Syntax: FOR operand1 = operand2 TO operand3 STEP operand4 statement ... END-FOR Example: DEFINE DATA LOCAL 1 #INDEX (I1) INIT <1> 1 #ROOT (N2.7) END-DEFINE FOR #INDEX 1 TO 5 COMPUTE #ROOT = SQRT (#INDEX) WRITE NOTITLE `=` #INDEX 3X `=` #ROOT END-FOR

IF Syntax: IF logical-condition THEN statement ... ELSE statement ... END-IF Example: IF SALARY (1) LT 40000 WRITE NOTITLE `*****` NAME 30X `SALARY LT 40000` ELSE IF BIRTH GT 300101 FIND VEHIC-VIEW WITH PERSONNEL-ID = PERSONNEL-ID (FND.) DISPLAY NAME BIRTH SALARY

Internal Use

15

Introduction to NATURAL

END-FIND END-IF END-IF MOVE Syntax: 1. MOVE ROUNDED operand1 (parameter) TO operand2 2. MOVE BY NAME operand1 TO operand2

3. MOVE BY POSITION operand1 TO operand2 4. MOVE EDITED operand1 TO operand2 (EM=value) 5. MOVE EDITED operand1 (EM=value) TO operand2 Examples: MOVE 'ABCDE' TO #A(A5) #B(A2) MOVE ROUNDED 1.995 TO #E(N1) #A: ABCDE #B: AB #E: 2

MOVE 'ABCDE'(PM=I) TO SUBSTRING (#F,1,4) #F: EDCB MOVE EDITED '003.45' TO #G (EM=999.99) MOVE RIGHT JUSTIFIED 'ABCDE' TO #H(A3) #G: 3.45 #H: CDE

RESET Syntax: RESET INITIAL operand1 ... Examples: DEFINE DATA LOCAL 1 #BINARY (B4) INIT <1> 1 #INTEGER (I4) INIT <5> 1 #NUMERIC (N2) INIT <25> END-DEFINE RESET NAME #BINARY #INTEGER #NUMERIC RESET INITIAL #BINARY #INTEGER #NUMERIC

Internal Use

16

Introduction to NATURAL

Sample NATURAL Program:DEFINE DATA LOCAL 01 #NAME (A25)

01 REDEFINE #NAME 02 # FORE-NAME (A10) 02 # SURNAME (A15)

01 #INPUT-RECORD

(N10)

01 REDEFINE #INPUT-RECORD 02 # INP-EMP-ID (N10)

01 #OUTPUT-RECORD (A80) 01 REDEFINE #OUTPUT-RECORD 02 #EMP-ID 02 #NAME (N10) (A25)

01 #EMP-SAL (N10.2) END-DEFINE

READ WORK FILE 1 #INPUT-RECORD MOVE LISA MOVE SAMUELS TO # FORE-NAME TO # SURNAME

MOVE # INP-EMP-ID TO #EMP-ID MOVE 3000000 TO #EMP-SAL WRITE WORK FILE 2 #OUTPUT-RECORD END-WORK END.

Internal Use

17

Introduction to NATURAL

Functional Differences between Structured and Reporting Modes:


The major functional differences between structured mode and reporting mode are summarized below:

Sl.no 1.

Structured Mode The syntax related to closing loops and functional blocks differs in the two modes. In structured mode, every loop or logical construct must be explicitly closed with a corresponding END...statement. Thus, it becomes immediately clear, which loop/logical constructs ends where. All data elements to be used have to be defined in one central location (either in the DEFINE DATA statement at the beginning of the program or in a data area outside the program).

Reporting Mode Reporting mode uses (CLOSE) LOOP and DO ... DOEND statements for this purpose. END-... statements (except END-DEFINE, ENDDECIDE and END-SUBROUTINE) cannot be used in Reporting mode, while LOOP and DO/DOEND statements cannot be used in structured mode.

2.

One can use database fields without having to define them in a DEFINE DATA statement; also, you can define user-defined variables anywhere in a program, which means that they can be scattered all over the program.

The Natural Statements documentation provides separate syntax diagrams for each modesensitive statement. The two examples below illustrate the differences between the two modes in constructing processing loops and logical conditions.

Reporting Mode Example: The reporting mode example uses the statements DO and DOEND to mark the beginning and end of the statement block that is based on the AT END OF DATA condition. The END statement closes all active processing loops. READ EMPLOYEES BY PERSONNEL-ID DISPLAY NAME BIRTH POSITION AT END OF DATA DO SKIP 2 WRITE / LAST SELECTED: OLD (NAME) DOEND END

Internal Use

18

Introduction to NATURAL

Structured Mode Example: The structured mode example uses an END-ENDDATA statement to close the AT END OF DATA condition, and an END-READ statement to close the READ loop. The result is a more clearly structured program in which you can see immediately where each construct begins and ends:

DEFINE DATA LOCAL 1 EMPL VIEW OF EMPLOYEES 2 PERSONNEL-ID 2 NAME 2 BIRTH 2 POSITION END-DEFINE READ EMPL BY PERSONNEL-ID DISPLAY NAME BIRTH POSITION AT END OF DATA SKIP 2 WRITE / LAST SELECTED: OLD (NAME) END-ENDDATA END-READ END

Books Referred:
NATURAL Developers Handbook version 4.1.3.

Internal Use

19

You might also like