-
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;
-
File processing with Dow
- File processing with Dow
- Read Customer;
- Dow Not %Eof(Customer); // %Eof checked here
- Count += 1;
- Write Detail;
- Read Customer;
- Enddo;
-
Do as long as condition is true
Dow - Do While
-
LEADING-decision loop (check condition , if true - do)
Dow - Do While
-
Condition is tested BEFORE the instructions w/i the loop are executed (in "Dow")
Dow - Do While
-
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
-
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
-
Do until condition will become true
Dou - Do Until
-
TRAILING-decision loop (do at least once, then check condition , if not - do again)
Dou - Do Until
-
Codnition is tested AFTER the instructions w/i the loop are executed (in "Enddo")
Dou - Do Until
-
For
- - count-controlled loop
- Processing will be done 25 times
- For Counter = 1 To 50 By 2;
- ...
- Endfor;
-
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
-
To avoid Numeric Overflow for Addition and Subtraction
- Zoned(3.2) + Zoned(6.3)
- result must be (6+1) Zoned(7.3)
-
To avoid Numeric Overflow for Multiplication
- Zoned(5.2) * Zoned(4.2)
- result must be (5+4) (2+2) Zoned(9.4)
-
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
-
Low order truncation
- ILE RPG automatically and w/o warning truncates extra decimal positions to fit the value in the result decimal positions
-
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
-
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;
-
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;
-
Readp
- - Read prior record
- Readp file-name {DS};
Readp Orders;
-
Readpe
- Read prior equal
Readpe search-argument file-name {DS};
Readpe Indate Orders;
-
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;
-
Chain
- - Random Access
- - To position to record (with reading)
Chain Indate Orders;
-
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;
-
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;
-
%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;
-
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;
-
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
-
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);
-
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;
-
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;
-
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;
-
Delete
Delete {seach-argument} file-name;
-
Delete record from the file
- Chain Custno Customers; // retrieve record
- . . .
- If %Found(Customers);
- Delete Customers; // delete retrieved record
- Endif;
-
Retrieve and delete record with one opcode
- Delete Custno Customers; // retrieve record
- // delete first and only one record
- //
with specified Custno
-
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
-
Usropn
- - Usropn keyword
- - 'User Open' to use Open and Close File in program
-
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
|
|