AZK RPG - Controlling Program Workflow

  1. Dow
    • Do While
    •    - do as long as condition is true
    •    - LEADING-decision loop (check condition , if true - do)
    •    - condition is tested BEFORE the instructions w/i the loop are executed (in "Dow")

    • Dow Number < 100;        // check here
    •    Number += 1;
    •    Sum += Number;
    • Enddo;
  2. File processing with Dow
    - File processing with Dow

    • Read Customer;
    • Dow Not %Eof(Customer);        // %Eof checked here
    •    Count += 1;
    •    Write Detail;
    •    Read Customer;
    • Enddo;
  3. Do as long as condition is true
    Dow - Do While
  4. LEADING-decision loop (check condition , if true - do)
    Dow - Do While
  5. Condition is tested BEFORE the instructions w/i the loop are executed (in "Dow")
    Dow - Do While
  6. Dou
    • - Do Until
    •    - do until condition will become true
    •    - TRAILING-decision loop (do at least once, then check condition , if not - do again)
    •    - condition is tested AFTER the instructions w/i the loop are executed (in "Enddo")

    • Dou Number = 100;
    •    Number += 1;
    •    Sum += Number;
    • Enddo;                // check here
  7. File processing with Dou
    - File processing with Dou

    • Dou %Eof(Customer);
    •    Read Customer;
    •    If %Eof(Customer);
    •       Leave
    •    Endif;
    •    Count += 1;
    •    Write Detail;
    • Enddo;                // %Eof checked here
  8. Do until condition will become true
    Dou - Do Until
  9. TRAILING-decision loop (do at least once, then check condition , if not - do again)
    Dou - Do Until
  10. Codnition is tested AFTER the instructions w/i the loop are executed (in "Enddo")
    Dou - Do Until
  11. For
    • - count-controlled loop
    • Processing will be done 25 times

    • For Counter = 1 To 50 By 2;
    •    ...
    • Endfor;
  12. High-order Overflow
    • - if the value to be stored is too large for the result field
    • - run-time error
    • - program stops and display an error message
  13. To avoid Numeric Overflow for Addition and Subtraction
    • Zoned(3.2) + Zoned(6.3)
    • result must be (6+1) Zoned(7.3)
  14. To avoid Numeric Overflow for Multiplication
    • Zoned(5.2) * Zoned(4.2)
    • result must be (5+4) (2+2) Zoned(9.4)
  15. To avoid Numeric Overflow for Division
    - if you divide by value less than 1, manually calculate with some representative values to get a sense of the size required to store result
  16. Low order truncation
    - ILE RPG automatically and w/o warning truncates extra decimal positions to fit the value in the result decimal positions
  17. Read
    - Sequential Access

    Read file-name {datastructure};

    • Dcl-f Customer Disk;
    •      // - result DS MUST use Likerec keyword
    • Dcl-ds Custdata Likerec(Custrec);

    • Read Customers;
    • If %Eof(Customers);
    •      // End of file processing
    • Endif;

    •      // - transfers data from file Customer to DS Custdata
    • Read Customers Custdata;
    • If %Eof(Customers);
    •      // End of file processing
    • Endif;

    - For program-described files, the DS's length must be same as file's record length
  18. Setll
    • - To position to first record (w/o reading)
    • - To check if record exists w/o reading the record

    •      // To position to first record (w/o reading)
    • Setll search-argument file-name

    • Setll *Loval Customers;
    • Read Customers;        // Read firs record

    •      // To check if record exists w/o reading the record
    • Setll Custchk Customers;
    • If %Equal(Customers);
    •    Exsr Validcust;        // cusromer exists
    • Else;
    •    Exsr Invalidcust;   // record not found
    • Endif;
  19. Reade
    • - Sequential Access
    • - read equal key
    • - usually used w Setll

    Reade search-argument file-name {DS};

    DS - result DS to receive the data (optional)

    • Setll Indate Orders;
    •    . . .
    • If %Equal(Orders);
    •    Reade Indate Orders;

    •    Dow Not %Eof(Orders);
    •       Write Orderline;
    •       Reade Indate Orders;
    •    Enddo;

    • Else;
    •    Exsr Noorders;
    • Endif;
  20. Readp
    • - Read prior record
    • Readp file-name {DS};

    Readp Orders;
  21. Readpe
    - Read prior equal

    Readpe search-argument file-name {DS};

    Readpe Indate Orders;
  22. Setgt
    - To read last record

    • Setgt *Hival Customers;
    • Readp Customers;    // Read last record

    - to determine the next order number to use

    • Setgt *Hival Orders;
    • Readp Orders;
    • If Not %Eof(Orders);     // last record found
    •    Ordernbr += 1;    
    • Else;             // no last record (empty file)
    •    Ordernbr = 1;
    • Endif;
  23. Chain
    • - Random Access
    • - To position to record (with reading)

    Chain Indate Orders;
  24. Chain to list all orders placed in Indate file
    • Chain Indate Orders;
    •    . . .
    • If %Found(Orders);
    •    Dow Not %Eof(Orders);
    •       Write Orderline;
    •       Reade Indate Orders;
    •    Enddo;
    • Else;
    •    Exsr Noorders;
    • Endif;
  25. Chain using composite keys
    Chain (Company:Region:Custno) Customers;

    Chain ('346' : Region : %Char(Lastcustno + 1)) Customers;

    - we can use partial composite keys

    Chain (Company:Region) Customers;
  26. %Kds
    - to specify Key DS (same as KEYLIST)

    %Kds(datastructure {:numb-of-keys})

    • - use 'datastructure' as the search argument of a composite key
    • - 'numb-of-keys' - to specify # of keys to use (partial key)

    • - DS to be used in %Kds
    •      Dcl-ds Custkeys;
    •         Company Char(3);
    •         Region Char(3);
    •         Custno Char(9);
    •      End-ds;

    • - Ext DS with "Extname" or "Likerec" keyword that automatically extrats only the key fields from the record format 
    •      Dcl-ds Custkeys Ext Extname('CUSTOMERS':*Key) End-ds;
    •      Dcl-ds Custkeys Likerec(Custrec:*Key) End-ds;

    • - Chain using all keys of Custkeys DS
    •      Chain %Kds(Custkeys) Customers;

    • - Chain using only 2 of 3 keys of Custkeys DS
    •      Chain %Kds(Custkeys:2) Customers;
  27. Chain using DS as the search argument of a composite key
    • Dcl-ds Custkeys;
    •    Company Char(3);
    •    Region Char(3);
    •    Custno Char(9);
    • End-ds;
    •    . . .
    • Chain %Kds(Custkeys) Customers;
  28. Write
    • - Random Access
    •     
    • Write file-name {DS};

    • - DS - datastructure
    •    - must be specified for Prog Desc File
    •    - optional for Ext Desc File

    • Dcl-f Customers Disk Usage(*Output);
    • Dcl-ds Custdata Likerec(Custrec:*Output);

    •             RF        DS
    • Write Custrec Custdata;

    • - Write transfers data from DS to the record
    • - Custsrec - Rec Format of Customer file
    • - Custdate - DS name
  29. Update
    • - modifies the record most recently read from an update file
    • - rewrites ALL of the a database record's fields to the file

    Update file-rec-name {DS);
  30. Update record in the file
    • Dcl-f Customers Dick Uage(*Update);
    •         . . .
    •    Chain Custno Customers;      
    •    If %Found(Customers):
    •        BalanceDue += InvoiceAmt;
    •        LastDate = InvoiceDate;
    •        Update Custrec;
    •    Else;
    •        Exsr NoCustomer;
    •    Endif;
  31. Update record directly from DS instead of field by field
    • Dcl-ds CustData Likerec(Custrec:*Input);
    •      . . .
    •         Chain Custno Customers;
    •         
    •         If %Found(Customers):
    •             BalanceDue += InvoiceAmt;
    •             LastDate = InvoiceDate;
    •             Update Custrec Custdata;
    •         Else;
    •             Exsr NoCustomer;
    •         Endif;
  32. Update only those fields that we want to rewrite
    • Dcl-f Customers Dick Uage(*Update);
    •         . . .
    •    Chain Custno Customers;      
    •    If %Found(Customers):
    •        BalanceDue += InvoiceAmt;
    •        LastDate = InvoiceDate;
    •        Update Custrec %Fields(BalanceDue:LastDate);
    •    Else;
    •        Exsr NoCustomer;
    •    Endif;
  33. Delete
    Delete {seach-argument} file-name;
  34. Delete record from the file
    • Chain Custno Customers;        // retrieve record
    •    . . .
    • If %Found(Customers);
    •    Delete Customers;    // delete retrieved record
    • Endif;
  35. Retrieve and delete record with one opcode
    • Delete Custno Customers;    // retrieve record
    •                         // delete first and only one record
    •                         // with specified Custno
  36. Locking record
    • - Two programs cannot access the same record in update mode
    •      - To Update the record use Chain, Read..., locking the record
    •      - W/o updating, use Chain(n), Read(n)..., w/o locking the record

    Chain Custno Customers;        // - LOCKS the record

    Chain(n) Custno Customers;    // - does NOT LOCK the record

    Unlock Customers;        // - UNLOCK the record
  37. Usropn
    • - Usropn keyword
    • - 'User Open' to use Open and Close File in program
  38. File Open and Close considerations
    Dcl-f Customers Disk Usropn;    // - to use Open and Close File in program

    Open Customers;            // - if Customer already opened - ERROR

    • If Not %Open(Customers);    // - to check if Customers is already opened
    •    Open Customers;
    • Endif;

    Close Customers;        // - if Customer already closed - NO Error

    Close *All;            // - close all opened files
Author
flashsmilenet
ID
340458
Card Set
AZK RPG - Controlling Program Workflow
Description
Controlling Program Workflow
Updated