Introduction to NATURAL.pdf

Embed Size (px)

Citation preview

  • 8/14/2019 Introduction to NATURAL.pdf

    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]

    mailto:[email protected]:[email protected]:[email protected]
  • 8/14/2019 Introduction to NATURAL.pdf

    2/19

    Introduction to NATURAL

    What is NATURAL?

    NATURAL is a 4th generation programming language. It is free format language whereit 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 Format Size

    Alphanumeric (Annn) 1-253 characters

    Numeric (Nnn.m) 1-29 digitsPacked decimal (Pnn.m) 1-29 digitsControl (C) 2-byte internal formatLogical (L) 1-byte FALSE/TRUE

    Date (D) P6Time (T) P12

    Floating Point (Fn) 4 bytes for single precision

    Integer (In) 8 bytes for double precision or 1,2 or 4 bytesBinary (B) 1-128 bytes

    Internal Use 2

  • 8/14/2019 Introduction to NATURAL.pdf

    3/19

    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 DATAstatement 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 3

  • 8/14/2019 Introduction to NATURAL.pdf

    4/19

    Introduction to NATURAL

    Example 1 - Fields Defined within a DEFINE DATA Statement:

    DEFINE DATA LOCAL

    01 #EMP-ID (N20)

    01 #EMP-NAME (A25)

    01 #EMP-ADDR (A20)

    01 #EMP-CITY (A25)

    01 #EMP-SAL (P9.2)

    END-DEFINE

    Example 2 - Fields Defined in a local data area outside the program.

    Sample LDA:

    01 #INPUT-FILE A 28

    R 01 #INPUT-FILE

    02 #GRP-NUM N 9.0

    02 #EMP-NO N 9.0

    02 #EMP-NAME A 10

    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 4

  • 8/14/2019 Introduction to NATURAL.pdf

    5/19

    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. Asubprogram 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 theinvoked 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 A 28

    R 01 # GRP-DET

    02 #GRP-NUM N 9.0

    02 #EMP-NO N 9.0

    02 #EMP-NAME A 10

    Program reference:

    DEFINE DATA

    PARAMETER USING SAMPLPDA

    END-DEFINE

    Internal Use 5

  • 8/14/2019 Introduction to NATURAL.pdf

    6/19

    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 (A1)

    2 SOURCE-SYSTEM

    2 C*CHANGE-HISTORY-GROUP

    2 CHANGE-HISTORY-GROUP (1:99)

    3 PREVIOUS-CHANGE-USER3 PREVIOUS-CHANGE-DATE

    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 (A25)

    01 #EMP-ID (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 6

  • 8/14/2019 Introduction to NATURAL.pdf

    7/19

    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 (A15)

    01 #EMP-ID (N10)

    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 7

  • 8/14/2019 Introduction to NATURAL.pdf

    8/19

    Introduction to NATURAL

    ADD

    Syntax:

    ADD ROUNDED operand1 ... TO operand2

    ADD ROUNDED operand1 ... GIVING operand2

    Example:

    ADD+5 -2 -1 GIVING #A #A: 2

    ADD.231 3.6 GIVING #B #B: 3.8

    ADDROUNDED 2.9 3.8 GIVING #C #C: 7

    SUBTRACT

    Syntax:

    SUBTRACT ROUNDED operand1 ... FROM operand2

    SUBTRACT ROUNDED operand1 ... FROM operand2 GIVING operand3

    Example:

    DEFINE DATA LOCAL

    1 #A (P2) INIT

    1 #B (P2)

    1 #C (P1.1) INIT

    END-DEFINESUBTRACT 6 FROM #A #A: 44

    SUBTRACT6 FROM 11 GIVING #A #A: 5

    SUBTRACT 3 4 FROM #A GIVING #B #A: 5 #B: -2

    SUBTRACT -3 -4 FROM #A GIVING #B #A: 5 #B: 12

    SUBTRACTROUNDED 2.06 FROM #C #C: 0.3

    MULTIPLY

    Syntax:MULTIPLY ROUNDED operand1 BY operand2

    MULTIPLY ROUNDED operand1 BY operand2 GIVING operand3

    Example:

    MULTIPLY#A BY 3 GIVING #C

    MULTIPLYROUNDED 3 BY 3.5 GIVING #C

    Internal Use 8

  • 8/14/2019 Introduction to NATURAL.pdf

    9/19

    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

    1 #B (N7)

    1 #C (N3.2)

    1 #D (N1)

    1 #E (N1) INIT

    1 #F (N1)END-DEFINE

    DIVIDE5 INTO #A GIVING #B #B: 4

    DIVIDE 3 INTO 3.1 GIVING #D #D: 1

    DIVIDE2 INTO #E REMAINDER #F #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:

    READEMPLOY-VIEW BY ISN STARTING FROM 1 ENDING AT 3

    DISPLAY PERSONNEL-ID NAME *ISN *COUNTER

    END-READ

    Internal Use 9

  • 8/14/2019 Introduction to NATURAL.pdf

    10/19

    Introduction to NATURAL

    READEMPLOY-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:

    FINDEMPLOY-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:

    STORERECORD IN EMPLOYEES

    WITH PERSONNEL-ID = #PERSONNEL-ID

    NAME = #NAME

    FIRST-NAME = #FIRST-NAME

    MAR-STAT = #MAR-STAT

    BIRTH = #BIRTH

    CITY = #CITY

    END TRANSACTION

    WRITE NOTITLE `RECORD HAS BEEN ADDED`

    Internal Use 10

  • 8/14/2019 Introduction to NATURAL.pdf

    11/19

    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

  • 8/14/2019 Introduction to NATURAL.pdf

    12/19

    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 FILE1 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 FILESyntax:

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

    Internal Use 12

  • 8/14/2019 Introduction to NATURAL.pdf

    13/19

    Introduction to NATURAL

    Example:

    FIND EMPLOY-VIEW WITH CITY = `LONDON`

    WRITE WORK FILE 1PERSONNEL-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

  • 8/14/2019 Introduction to NATURAL.pdf

    14/19

    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

  • 8/14/2019 Introduction to NATURAL.pdf

    15/19

    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 #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:

    IFSALARY (1) LT 40000

    WRITE NOTITLE `*****` NAME 30X `SALARY LT 40000`

    ELSE

    IFBIRTH GT 300101

    FIND VEHIC-VIEW WITH PERSONNEL-ID = PERSONNEL-ID (FND.)

    DISPLAY NAME BIRTH SALARY

    Internal Use 15

  • 8/14/2019 Introduction to NATURAL.pdf

    16/19

    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) #A: ABCDE #B: ABMOVE ROUNDED 1.995 TO #E(N1) #E: 2

    MOVE'ABCDE'(PM=I) TO SUBSTRING (#F,1,4) #F: EDCB

    MOVEEDITED '003.45' TO #G (EM=999.99) #G: 3.45

    MOVERIGHT JUSTIFIED 'ABCDE' TO #H(A3) #H: CDE

    RESET

    Syntax:

    RESET INITIAL operand1 ...

    Examples:

    DEFINE DATA LOCAL

    1 #BINARY (B4) INIT

    1 #INTEGER (I4) INIT

    1 #NUMERIC (N2) INIT

    END-DEFINE

    RESETNAME #BINARY #INTEGER #NUMERIC

    RESETINITIAL #BINARY #INTEGER #NUMERIC

    Internal Use 16

  • 8/14/2019 Introduction to NATURAL.pdf

    17/19

    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 (N10)

    02 #NAME (A25)

    01 #EMP-SAL (N10.2)

    END-DEFINE

    READ WORK FILE 1 #INPUT-RECORD

    MOVE LISA TO # FORE-NAME

    MOVE SAMUELS 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

  • 8/14/2019 Introduction to NATURAL.pdf

    18/19

    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 Structured Mode Reporting Mode

    1. 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.

    Reporting mode uses (CLOSE) LOOP and DO ...

    DOEND statements for this purpose.

    END-... statements (except END-DEFINE, END-

    DECIDE and END-SUBROUTINE) cannot be used in

    Reporting mode, while LOOP and DO/DOEND

    statements cannot be used in structured mode.

    2. All data elements to be used have to bedefined in one central location (either

    in the DEFINE DATA statement at the

    beginning of the program or in a data

    area outside the program).

    One can use database fields without having todefine 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 mode-

    sensitive 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

  • 8/14/2019 Introduction to NATURAL.pdf

    19/19

    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.

    I t l U 19