IF Statement in COBOL COBOL

The IF statement causes a specified condition to be evaluated.The subsequent action of the object program depends on whether the value of the condition is true or false.

IF Statement in COBOL

statement-1 and statement-2 each represent either an imperative statement or a conditional statement optionally preceded by an imperative statement.

The scope of an IF statement is terminated by any of the following:

  • An END-IF phrase at the same level of nesting.
  • A separator period.
  • If nested, by an ELSE phrase associated with an IF statement at a higher level of nesting.
  • The next phrase of any statement in which the IF statement is contained. When an IF statement is executed, the following transfers of control occur:
  • If condition-1 is true, statement-1 is executed if specified.If statement-1 contains a procedure branching or conditional statement, control is explicitly transferred in accordance with the rules of that statement.If statement-1 does not contain a procedure branching or conditional statement, the ELSE phrase, if specified, is ignored and control passes to the end of the IF statement.
  • If condition-1 is true and the NEXT SENTENCE phrase is specified instead of statement-1, the ELSE phrase, if specified, is ignored and control passes to the next executable sentence.
  • If condition-1 is false, statement-1 or its surrogate NEXT SENTENCE is ignored, and statement-2, if specified, is executed.If statement-2 contains a procedure branching or conditional statement, control is explicitly transferred in accordance with the rules of that statement; otherwise, upon the completion of statement-2 control passes to the end of the IF statement.If the ELSE statement-2 phrase is not specified, statement-1 is ignored and control passes to the end of the IF statement.
  • If condition-1 is false, and the ELSE NEXT SENTENCE phrase is specified, statement-1 is ignored, if specified, and control passes to the next executable sentence. Either statement-1 or statement-2 may contain an IF statement.When this occurs, the IF statement is said to be nested.

IF statements within IF statements may be considered as paired IF and ELSE and END-IF combinations, proceeding from left to right.Thus, any ELSE or END-IF encountered is considered to apply to the most recent preceding IF that has not been already paired with an ELSE or END-IF.

IF Statement Examples



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics