Example:-
Below example will demonstrate use of NEXT SENTENCE. Please note that in below example, we are using program logic as used in example of CONTINUE statement (in previous topic). This will help you understand the difference between NEXT SENTENCE and CONTINUE.
In PROCEDURE DIVISION,
MOVE 10000 TO EMP-SALARY.
IF EMP-GRADE = ‘C’ THEN
NEXT SENTENCE
ELSE
ADD 1000 TO EMP-SALARY
END-ID
DISPLAY ‘NEW SALARY: ‘ EMP-SALARY.
DISPLAY ‘EMP-GRADE: ‘ EMP-GRADE.
Let say, in above code snippet, EMP-GRADE can be either ‘A’, ’B’ or ‘C’. Now, since we have used NEXT SENTENCE in THEN part of IF statement, if suppose EMP-GRADE=’C’, then control will be transferred to next sentence i.e. directly EMP-GRADE will be displayed. NEW-SALARY will not be displayed because that DISPLAY statement is part of sentence where IF and NEXT SENTENCE is coded. That sentence ends by period after DISPLAY of NEW-SALARY
In above example, it makes more sense to use NEXT SENTENCE instead of CONTINUE because definitely when there is no increment for grade ‘C’ employees, it makes no sense to display their New Salary since they already know their default salary.
SYSOUT display (output) when EMP-GRADE = ‘A’:-
NEW-SALARY: 11000
EMP-GRADE: A
SYSOUT display (output) when EMP-GRADE = ‘C’:-
EMP-GRADE: C