sign in
 
   
 
 
 
   
  COBOL TUTORIAL FOR BCA STUDENTS OF M G UNIVERSITY  
  UNIT 4: PROCEDURE DIVISION . . .  
   
     
 

CONDITIONAL STATEMENTS

 
     
  IF STATEMENT  
 

The simple form of IF statement is:

 
   
 

If the condition-1 becomes true then statement-1 will be executed. Statement-1 can be a simple statement or a compound statement. If the condition fails then control goes out to the next statement.

Example:


 
 

IF A > B DISPLAY “ A IS BIG”.
IF BASIC-PAY NOT > 5000 GO TO TEST-PARA.

 
   
   
  IF … ELSE STATEMENT  
 

The general format of the IF statement is as follows:

 
   
 

The condition can be any one of the conditions discussed above. Each of statement-1 and statement-2 represents one or more COBOL statement. When more then one statement is specified they must be separated by one or more spaces or by an optional semicolon (;) or comma (,). During execution, if the condition is found to be true, the statements represented by statement-1 are executed. On the other hand, if the condition is found to be false, the statements represented by statement-2 are executed. For ease of reference, we shall call the statements represented by statement-1 and statement-2 as then part and else part respectively.

It may be noted that either the then part or else part is executed depending on the value of the specified condition. After that the control implicitly goes to the statement that immediately follows the IF sentence.

Sometimes, we encounter situations where no action needs to be specified if the condition is true, but some actions are necessary if the condition is false. In that case, the NEXT SENTENCE phrase can be used for the then part and the else part can be written to the indicate the actions required. Similarly, the NEXT SENTENCE phrase can replace the else part if no action is required when the condition is false. The NEXT SENTENCE phrase indicates that the control should pass to the statement that follows the IF sentence. Note that if no action needs to be specified for the else part, the phrase ELSE NEXT SENTENCE, being optional, can be omitted. It is in this form that we have used the IF statement so for. However, the phrase ELSE NEXT SENTENCE may not be omitted in certain cases.

Example:

 
   
 

The specified condition tests whether or not the current value of the data name QUANTITY is numeric as well as positive. If the condition is true ERROR-CODE is set to zero and SALES-VALUE is computed by multiplying QUANTITY by RATE. On the other hand, if the condition is FALSE, ERROR CODE is set to 1 and SALES-VALUE is set to zero. In either case the control goes implicitly to the next statement after this IF sentence.