MainframeMaster

COBOL Tutorial

COBOL NEXT SENTENCE - Quick Reference

Progress0 of 0 lessons

Overview

The NEXT SENTENCE statement in COBOL transfers control to the statement following the next period (sentence terminator) in the program. It is commonly used within IF statements to skip the execution of subsequent statements.

Purpose and Usage

  • Control flow transfer to next sentence
  • Skip execution of subsequent statements
  • Conditional execution within IF statements
  • Legacy code support for older programs

Syntax

The NEXT SENTENCE statement has a simple syntax:

Basic Syntax

cobol
1
NEXT SENTENCE.

NEXT SENTENCE transfers control to the statement after the next period.

Example: Within IF Statement

cobol
1
2
3
4
5
6
7
IF condition NEXT SENTENCE ELSE PERFORM some-procedure. DISPLAY "This will be executed if condition is false.". * Control transfers here if condition is true

Example: Multiple Conditions

cobol
1
2
3
4
5
6
7
8
9
10
IF condition1 NEXT SENTENCE ELSE IF condition2 NEXT SENTENCE ELSE PERFORM default-procedure. DISPLAY "Default action taken.". * Control transfers here if either condition is true

Practical Examples

Here are some practical uses of NEXT SENTENCE in COBOL:

Input Validation

cobol
1
2
3
4
5
6
7
8
9
10
11
VALIDATE-INPUT. IF input-field = SPACES NEXT SENTENCE ELSE IF input-field IS NOT NUMERIC NEXT SENTENCE ELSE PERFORM PROCESS-VALID-INPUT. DISPLAY "Input processed successfully.". * Control continues here if input is invalid

Skips processing if input validation fails.

Error Handling

cobol
1
2
3
4
5
6
7
8
PROCESS-RECORD. IF record-status = "ERROR" NEXT SENTENCE ELSE PERFORM PROCESS-VALID-RECORD. DISPLAY "Record processed.". * Error records are skipped, control continues here

Skips processing of error records.

Best Practices

  • Use NEXT SENTENCE sparingly and mainly for legacy code maintenance.
  • Prefer structured IF-END-IF blocks for new code.
  • Use CONTINUE for empty ELSE clauses instead of NEXT SENTENCE.
  • Document the control flow when NEXT SENTENCE is used.
  • Consider refactoring code to use structured programming constructs.

Common Pitfalls

  • Difficult to follow control flow in complex nested structures.
  • Can lead to spaghetti code and maintenance issues.
  • May not work as expected in deeply nested IF statements.
  • Makes debugging more challenging.

Test Your Knowledge

1. What does the NEXT SENTENCE statement do in COBOL?

  • Moves to the next paragraph
  • Skips to the next sentence after the current IF statement
  • Continues to the next line
  • Jumps to the next section

2. Where is NEXT SENTENCE typically used?

  • In the DATA DIVISION
  • Within IF statements for control flow
  • In file operations
  • In arithmetic operations

3. What is the difference between NEXT SENTENCE and CONTINUE?

  • They are the same
  • NEXT SENTENCE skips to next sentence, CONTINUE does nothing
  • CONTINUE is obsolete
  • NEXT SENTENCE is faster

4. Is NEXT SENTENCE considered good practice in modern COBOL?

  • Yes, always
  • No, it is discouraged in favor of structured programming
  • Only in legacy code
  • Only in specific situations

5. What happens if NEXT SENTENCE is used outside an IF statement?

  • It causes a compile error
  • It skips to the next sentence anyway
  • It has no effect
  • It terminates the program

Frequently Asked Questions