Continue and Next Sentence in COBOL
Continue and Next Sentence in COBOL are No Operation statements which does nothing except to pass control to the statement after the scope terminators. But the difference is the place that it passes the control to.
CONTINUE:
Continue gives the control after the next explicit scope terminator. Explicit Scope terminators are END-IF, END-PERFORM, END-EVALUATE, etc.
Example 1:
OPEN INPUT FILE1 IF WW-FILE-STATUS = ‘00’ CONTINUE ELSE DISPLAY ‘ FILE OPEN FAILED’ END-IF READ FILE1
In the above code, continue does nothing but it passes control to the statement after end-if.
Example 2:
IF EMP-NUM > ZEROES CONTINUE ELSE PERFORM P100-GET-NEXT-EMPL-NUM END-IF P200-UPDATE-EMPL-IN-DB
In the above code, the requirement is to update the employee data in a Database for a valid employee number.
NEXT SENTENCE:
It gives control to the next implicit scope terminator which is a period.
Example:
IF WW-VAR1 = WW-VAR2 NEXT SENTENCE ELSE MOVE WW-VAR-3 TO WW-VAR1 END-IF IF WW-VAR1 = WW-VAR4 DISPLAY 'MATCHED' ELSE MOVE WW-VAR4 TO WW-VAR1 END-IF. -----> Implicit Scope terminator. PERFORM P100-BUSINESS-LOGIC
When variable 1 is equal to variable 2 then checking the other IF statements are not required and business logic needs to be performed. So the Next Sentence gives control directly to P100 para which is after the implicit scope terminator(.).
What will happen if the continue is used in nested-if?
IF EMP-NUM > ZEROES
CONTINUE
ELSE
IF EMP-AGE > 25
PERFORM P100-PROCESS-AGE-L25
ELSE
PERFORM P200-PROCESS-AGE-G25
END-IF
END-IF
PERFORM P050-GET-NEXT-EMPNUM.
Will the control comes next to nested END-IF or corresponding END-IF?
It will execute P050-GET-NEXT-EMPNUM para in this case.
What if there is a nested if and continue is used ? will the control gets passed to the last end-if or
immediate nested end-if. please clarify
As per above explanation it goes to immediate Inner nested End-If .
nice question and explanations for the mainframe interview