You are on page 1of 25

Flow control

 IF
 DECIDE ON
 DECIDE FOR
 FOR
 REPEAT
 PERFORM
 CALLNAT
 FETCH
 STOP
IF statement
 Example 1: IF statement

DEFINE DATA
LOCAL
.
1 #CORP-NAME (A30)
1 #CORP-NBR (N1)
1 #DAYS (P3)
.
END-DEFINE
.
.
IF #CORP-NBR = 1
#CORP-NAME := ‘Entropy Corporation’
#DAYS := 30
END-IF
 Example 2: IF with ELSE

DEFINE DATA
LOCAL
.
1 #DAYS (P3)
1 #GOOD-CLIENT (L)
.
END-DEFINE
.
.
IF #GOOD-CLIENT
#DAYS := 35
ELSE
#DAYS := 28
END-IF
 Example 3: Nested IF with ELSE

DEFINE DATA
LOCAL
.
1 #CORP-NAME (A30)
1 #CORP-NBR (N1)
.
END-DEFINE
.
.
IF #CORP-NBR = 1
#CORP-NAME := ‘Northern Enterprises’
ELSE
IF #CORP-NBR = 2
#CORP-NAME := ‘Southern Hospitality Corp’
ELSE
IF #CORP-NBR = 3
#CORP-NAME := ‘Eastern Partnership’
ELSE
#CORP-NAME := ‘Western Vista’
END-IF
END-IF
END-IF
 Example 4: IF with compound conditions

DEFINE DATA
LOCAL
.
1 #CORP-NBR (N1)
1 #DAYS (P3)
1 #GOOD-CLIENT (L)
.
END-DEFINE
.
.
IF (#CORP-NBR = 1) AND (#GOOD-CLIENT = TRUE)
#DAYS := 35
ELSE
#DAYS := 28
END-IF
 DECIDE ON statement

 Example 1: DECIDE ON

DEFINE DATA
LOCAL
.
1 #CORP-NAME (A30)
1 #CORP-NBR (N1)
.
END-DEFINE
.
.
DECIDE ON FIRST VALUE OF #CORP-NBR
VALUE 1
#CORP-NAME := ‘Northern Enterprises’
VALUE 2
#CORP-NAME := ‘Southern Hospitality Corp’
VALUE 3
#CORP-NAME := ‘Eastern Partnership’
NONE
#CORP-NAME := ‘Western Vista’
END-DECIDE
 Example 2: DECIDE ON with IGNORE statement

DEFINE DATA
LOCAL
.
1 #CORP-NAME (A30)
1 #CORP-NBR (N1)
.
END-DEFINE
.
.
DECIDE ON FIRST VALUE OF #CORP-NBR
VALUE 1
#CORP-NAME := ‘Northern Enterprises’
VALUE 2
#CORP-NAME := ‘Southern Hospitality Corp’
VALUE 3
#CORP-NAME := ‘Eastern Partnership’
VALUE 4
#CORP-NAME := ‘Western Vista’
NONE
IGNORE
END-DECIDE
DECIDE FOR statement
 Example 1: DECIDE FOR FIRST

DEFINE DATA
LOCAL
.
1 #CORP-NAME (A30)
1 #CORP-NBR (N1)
.
END-DEFINE
.
DECIDE FOR FIRST CONDITION
WHEN #CORP-NBR = 1
#DAYS := 28
WHEN #GOOD-CLIENT = TRUE
#DAYS := 35
WHEN NONE
#DAYS := 30
END-DECIDE

 In a DECIDE FOR statement the sequence of conditions is very significant.


Conditions are evaluated sequentially, i.e. from the top down.
 Example 2: DECIDE FOR EVERY
DEFINE DATA
LOCAL
.
1 #CORP-NAME (A30)
1 #CORP-NBR (N1)
.
END-DEFINE
.
.
DECIDE FOR EVERY CONDITION
WHEN #CORP-NBR = 1
#DAYS := 28
WHEN #GOOD-CLIENT = TRUE
#DAYS := 35
WHEN NONE
#DAYS := 30
END-DECIDE

 when a condition is satisfied, the specified actions will be taken and additionally every
subsequent condition will be evaluated until END-DECIDE is reached.
 Example 3: DECIDE FOR EVERY with ANY and ALL clauses

DEFINE DATA
LOCAL
.
1 #BLUE (L) INIT<TRUE>
1 #RED (L) INIT<FALSE>
1 #YELLOW (L) INIT<TRUE>
.
END-DEFINE
.
DECIDE FOR EVERY CONDITION
WHEN #BLUE
WRITE ‘We have blue paint’
WHEN #RED
WRITE ‘We have red paint’
WHEN #YELLOW
WRITE ‘We have yellow paint’
WHEN #BLUE AND #RED
WRITE ‘We can make purple’
WHEN #BLUE AND #YELLOW
WRITE ‘We can make green’
WHEN #RED AND #YELLOW
WRITE ‘We can make orange’
WHEN ANY
WRITE ‘Yes, we have paint’
WHEN ALL
WRITE ‘We can make brown’
WHEN NONE
WRITE ‘No, we don’t have paint’
END-DECIDE
REPEAT STATEMENTS
 UNTIL option : The processing loop will be continued
until the logical condition becomes true.
 WHILE option : The processing loop will be continued
as long as the logical condition is true.
example
DEFINE DATA LOCAL
1 #X (I1) INIT <0>
1 #Y (I1) INIT <0>
END-DEFINE
*
REPEAT WHILE #X <= 5
ADD 1 TO #X
WRITE NOTITLE '=' #X
END-REPEAT
*
SKIP 3
REPEAT
ADD 1 TO #Y
WRITE '=' #Y
UNTIL #Y = 6
END-REPEAT
*
END
output
 #X: 1
 #X: 2
 #X: 3
 #X: 4
 #X: 5
 #X: 6

 #Y: 1
 #Y: 2
 #Y: 3
 #Y: 4
 #Y: 5
 #Y: 6
ESCAPE TOP
 ESCAPE TOP terminates the current iteration of a
processing loop so that the next iteration can begin.
 Control passes to the top of the loop.
DEFINE DATA
.
1 #INSTALLMENT-NBR (P2)
.
1 INVOICE
2 CLIENT-ID (N8)
.
2 NBR-INSTALLMENTS (N2)
2 DUE-AMOUNT (N7.2/12)
.
.
END-DEFINE
.
.
READ WORK FILE 1 INVOICE
.
FOR #INSTALLMENT-NBR = 1 TO
INVOICE.NBR-INSTALLMENTS
IF INVOICE.DUE-AMOUNT(#INSTALLMENT-
NBR) = 0
ESCAPE TOP
END-IF
.
.
END-FOR
.
END-WORK
.
ESCAPE TOP REPOSITION
 When an ESCAPE TOP REPOSITION statement is
executed, Natural immediately continues processing at
the top of the active READ loop, using the current
value of the search variable as new start value.
 At the same time, ESCAPE TOP REPOSITION resets
the system variable *COUNTER to zero.
ESCAPE BOTTOM
 ESCAPE BOTTOM terminates the current processing
loop. Control passes to the statement following the
bottom of the loop.
ESCAPE BOTTOM (r)
 If BOTTOM is followed by a label or reference number,
processing will continue with the first statement
following the processing loop identified by the label or
reference number.
 A label or a reference number can only be specified if
the ESCAPE BOTTOM statement is physically placed
within the referenced processing loop.
DEFINE DATA
.
1 RECEIVABLE VIEW OF RECEIVABLE
2 CLIENT-ID
2 UNPAID-FLAG
2 PRODUCT-ID
.
END-DEFINE
.
READ RECEIVABLE BY CLIENT-ID
.
IF RECEIVABLE.CLIENT-ID > 9999
ESCAPE BOTTOM
END-IF
.
.
.
END-READ
.
ESCAPE BOTTOM IMMEDIATE
 If you specify the keyword IMMEDIATE, no loop-end
processing will be performed.
 Loop-end processing refers to AT END OF DATA and
AT BREAK statements. The keyword IMMEDIATE
means that you don't want Natural to invoke the AT
END OF DATA nor to force a final break.
DEFINE DATA LOCAL
1 EMP VIEW EMPLOYEES
2 PERSONNEL-ID
END-DEFINE
READ EMP BY PERSONNEL-ID FROM
'11100108'
AT BREAK OF PERSONNEL-ID /7/
WRITE 5X '1 break'
END-BREAK
AT END OF DATA
WRITE 5X '1 end data'
END-ENDDATA 1 11100108
/* 1 break
WRITE '1' PERSONNEL-ID 1 11100109
IF *COUNTER = 5 1 break
THEN 1 11100110
ESCAPE BOTTOM 1 break
END-IF 1 11100111
END-READ 1 break
END 1 11100112
1 break
1 end data
DEFINE DATA LOCAL
1 EMP VIEW EMPLOYEES
2 PERSONNEL-ID
END-DEFINE
READ EMP BY PERSONNEL-ID FROM '11100108'
AT BREAK OF PERSONNEL-ID /7/
WRITE 5X '2 break'
END-BREAK
AT END OF DATA
WRITE 5X '2 end data'
END-ENDDATA 2 11100108
/* 2 break
WRITE '2' PERSONNEL-ID 2 11100109
IF *COUNTER = 5 2 break
THEN 2 11100110
ESCAPE BOTTOM IMMEDIATE 2 break
END-IF 2 11100111
END-READ
END
2 break
2 11100112
ESCAPE ROUTINE
 This option indicates that the current Natural routine,
which may have been invoked via a PERFORM,
CALLNAT, FETCH RETURN, or as a main program, is
to relinquish control.
 In the case of a subroutine, processing will continue
with the first statement after the statement used to
invoke the subroutine. In the case of a main program,
Natural command mode will be entered.
 All loops currently active within the routine will be
terminated and loop-end processing performed.
ESCAPE MODULE
 This option indicates that the entire current program
level, with all internal subroutines, is to relinquish
control.
 As with ESCAPE ROUTINE, loop-end processing will
be performed. However, if you specify the keyword
IMMEDIATE, no loop-end processing will be
performed.

You might also like