IF statements in COBOL

1 / 30

# IF statements in COBOL - PowerPoint PPT Presentation

IF statements in COBOL. Please open the speaker notes - they contain additional information!. Simple IF - processing on Y only. IF AMT > 5000. N. Y. AMT x 10 = ANS-WS. If the data in the AMT field is greater than 5000, we want to multiply the data by 10 and put the answer in ANS-WS.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'IF statements in COBOL' - benjamin

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### IF statements in COBOL

Simple IF - processing on Y only

IF AMT > 5000

N

Y

AMT x 10

= ANS-WS

If the data in the AMT field is greater than 5000, we want to multiply the data by 10 and put the answer in ANS-WS.

IF AMT > 5000

MULTIPLY AMT BY 10

GIVING ANS-WS.

You can end the IF statement with a period or you can end it with an END-IF.

IF AMT > 5000

MULTIPLY AMT BY 10

GIVING ANS-WS

END-IF.

Comparison operators

• Valid relational operators are:
• (Note: IS optional)
• IS [NOT] EQUAL TOIS [NOT] =IS [NOT] GREATER THANIS [NOT] IS [NOT] LESS THANIS [NOT] <IS GREATER THAN OR EQUAL TOIS =IS LESS THAN OR EQUAL TOIS <=

Simple IF - processing on Y or N

IF AMT > 5000

N

Y

AMT x 5

= ANS-WS

AMT x 10

= ANS-WS

IF AMT > 5000

MULTIPLY AMT BY 10

GIVING ANS-WS

ELSE

MULTIPLY AMT BY 5

GIVING ANS-WS.

In this example, if the AMT is greater than 5000

then we want to multiply AMT by 10 and store

the answer in ANS-WS. If the AMT is not greater

than 5000 then we want to multiply AMT by 5 and

IF AMT > 5000

MULTIPLY AMT BY 10

GIVING ANS-WS

ELSE

MULTIPLY AMT BY 5

GIVING ANS-WS

END-IF

Note: An IF statement can be terminated by a period or the END-IF command.

Simple IF - multiple processing steps

IF AMT > 5000

N

Y

AMT x 5

= ANS-WS

AMT x 10

= ANS-WS

MOVE “LESS”

TO MSG-PR

MOVE

“GREATER”

TO MSG-PR

IF AMT > 5000

MULTIPLY AMT BY 10

GIVING ANS-WS

MOVE “GREATER” TO MSG-PR

ELSE

MULTIPLY AMT BY 5

GIVING ANS-WS

MOVE “LESS” TO MSG-PR

END-IF

IF AMT > 5000

MULTIPLY AMT BY 10

GIVING ANS-WS

MOVE “GREATER” TO MSG-PR

ELSE

MULTIPLY AMT BY 5

GIVING ANS-WS

MOVE “LESS” TO MSG-PR.

Simple IF - PERFORM processing

IF AMT > 5000

N

Y

PERFORM

B-310-LS-ROUT

PERFORM

B-300-GR-ROUT.

IF AMT > 5000

PERFORM B-300-GR-ROUT

ELSE

PERFORM B-310-LS-ROUT

END-IF

IF AMT > 5000

PERFORM B-300-GR-ROUT

ELSE

PERFORM B-310-LS-ROUT.

IF with NEXT SENTENCE

IF ANS-WS

> 100

N

Y

MOVE ANS-WS

TO ANS-PR

IF ANS-WS > 100

NEXT SENTENCE

ELSE

MOVE ANS-WS TO ANS-PR.

IF ANS-WS > 100

NEXT SENTENCE

ELSE

MOVE ANS-WS TO ANS-PR

END-IF

IF with NOT

IF ANS-WS

NOT > 100

N

Y

MOVE ANS-WS

TO ANS-PR

IF ANS-WS NOT > 100

MOVE ANS-WS TO ANS-PR.

IF ANS-WS NOT > 100

MOVE ANS-WS TO ANS-PR

END-IF

AND relationship

IF INVCD = “A”

N

Y

IF AMT > 5000

Y

N

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A”

IF AMT > 5000

MOVE “OKAY” TO MSG-PR.

IF INVCD = “A”

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

END-IF

END-IF

The IF statement can be terminated with a period or an END-IF for each IF used.

Combined/compound AND

IF INVCD = “A”

N

Y

IF AMT > 5000

Y

N

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A” AND AMT > 5000

MOVE “OKAY” TO MSG-PR

END-IF

IF INVCD = “A” AND AMT > 5000

MOVE “OKAY” TO MSG-PR.

AND relationship - includes processing if not true

IF INVCD = “A”

N

Y

IF AMT > 5000

Y

N

MOVE “ERROR”

TO MSG-PR

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A”

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

IF INVCD = “A”

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

Combined/compound AND

IF INVCD = “A”

N

Y

IF AMT > 5000

Y

N

MOVE “ERROR”

TO MSG-PR

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A” AND AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

IF INVCD = “A” AND AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

INVALID AND code

IF INVCD = “A”

N

Y

IF AMT > 5000

Y

N

MOVE “NOT A”

TO MSG-PR

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A” AND AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

IF INVCD = “A” AND AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

The problem is the MOVE after the ELSE does not know which question received an answer of NO and therefore can’t mvoe the appropriate message.

IF with different processing for NO response

IF INVCD = “A”

N

Y

IF AMT > 5000

Y

N

MOVE “NOT A”

TO MSG-PR

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A”

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

ELSE

MOVE “NOT A” TO MSG-PR.

IF INVCD = “A”

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

ELSE

MOVE “NOT A” TO MSG-PR

END-IF

OR relationship

IF INVCD = “A”

N

Y

MOVE “OKAY”

TO MSG-PR

IF AMT > 5000

Y

N

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A”

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT > 5000

MOVE “OKAY” TO MSG-PR.

IF INVCD = “A”

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

END-IF

END-IF

Combined/compound OR

IF INVCD = “A”

N

Y

MOVE “OKAY”

TO MSG-PR

IF AMT > 5000

Y

N

MOVE “OKAY”

TO MSG-PR

IF INVCD = “A” OR AMT > 5000

MOVE “OKAY” TO MSG-PR.

IF INVCD = “A” OR AMT > 5000

MOVE “OKAY” TO MSG-PR

END-IF

OR with Yes and No processing

IF INVCD = “A”

N

Y

MOVE “OKAY”

TO MSG-PR

IF AMT > 5000

Y

N

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

IF INV-CD = “A”

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

END-IF

IF INV-CD = “A”

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

IF INVCD = “A”

OR compound with Y/N processing

N

Y

MOVE “OKAY”

TO MSG-PR

IF AMT > 5000

Y

N

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

IF INV-CD = “A” OR AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

IF INV-CD = “A” OR AMT > 5000

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

IF INVCD = “A”

OR with different processing

N

Y

MOVE “OK - A”

TO MSG-PR

IF AMT > 5000

Y

N

MOVE “ERROR”

TO MSG-PR

MOVE “OK - AMT”

TO MSG-PR

IF INV-CD = “A”

MOVE “OK-A” TO MSG-PR

ELSE

IF AMT > 5000

MOVE “OK-AMT” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

END-IF

IF INV-CD = “A”

MOVE “OK-A” TO MSG-PR

ELSE

IF AMT > 5000

MOVE “OK - AMT” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

IF INVCD = “A”

INVALID OR with different processing

N

Y

MOVE “OK - A”

TO MSG-PR

IF AMT > 5000

Y

N

MOVE “ERROR”

TO MSG-PR

MOVE “OK - AMT”

TO MSG-PR

IF INV-CD = “A” or AMT > 5000

MOVE “OK-A” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

IF INV-CD = “A” OR AMT > 5000

MOVE “OK-A” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

cond1 AND (cond 2 or cond3)

IF INVCD = “A”

N

Y

IF AMT-FST > 500

Y

N

MOVE “OKAY”

TO MSG-PR

IF AMT-SND > 200

N

Y

MOVE “OKAY”

TO MSG-PR

Rules for combined/compound

• When you combine AND, OR and NOT, the following rules must be followed:
• Evaluation works from left to right
• Parenthesis are resolved first - inner parenthesis are resolved before outer parenthesis
• NOT evaluated next
• AND and the conditions around it evaluated next
• OR and the conditions around it are evaluated last

Cond1 AND (cond2 OR cond3) - COBOL code

Code using PERIOD:

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OKAY” TO MSG-PR.

A single period ends all of the IF statements.

END-IF needs an END-IF for each IF statement.

There are times when both styles are appropriate. Today’s coding style emphasized the END-IF

Code using END-IF:

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OKAY” TO MSG-PR

END-IF

END-IF

END-IF

When dealing with compound AND and OR it must be noted that AND gets resolved before OR. If you want to resolve the OR first, you must enclose it in parenthesis.

Compound AND/OR (2 versions):

IF INVCD = “A” AND (AMT-FST > 500 OR AMT-SND > 200)

MOVE “OKAY” TO MSG-PR.

IF INVCD = “A” AND (AMT-FST > 500 OR AMT-SND > 200)

MOVE “OKAY” TO MSG-PR

END-IF

Compound AND/OR with different processing for Y/N

IF INVCD = “A”

N

Y

IF AMT-FST > 500

MOVE “ERROR”

TO MSG-PR

Y

N

MOVE “OKAY”

TO MSG-PR

IF AMT-SND > 200

N

Y

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

CODE for AND/OR

SIMPLE AND/OR IF

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

END-IF

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

SIMPLE AND/OR IF

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

COMPOUND AND/OR IF (2 versions)

IF INVCD = “A” AND (AMT-FST > 500 OR AMT-SND > 200)

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR.

IF INVCD = “A” AND (AMT-FST > 500 OR AMT-SND > 200)

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

AND/OR - different NO processing

IF INVCD = “A”

N

Y

IF AMT-FST > 500

MOVE “NOT A”

TO MSG-PR

Y

N

MOVE “OKAY”

TO MSG-PR

IF AMT-SND > 200

N

Y

MOVE “ERROR”

TO MSG-PR

MOVE “OKAY”

TO MSG-PR

SIMPLE AND/OR IF

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

END-IF

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

AND/OR - different NO processing

SIMPLE AND/OR IF

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OKAY” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

ELSE

MOVE “NOT A” TO MSG-PR.

COMPOUND AND/OR IF (2 versions)

IF INVCD = “A”

IF AMT-FST > 500 OR AMT-SND > 200

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

ELSE

MOVE “NOT A” TO MSG-PR.

IF INVCD = “A”

IF AMT-FST > 500 OR AMT-SND > 200

MOVE “OKAY” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

ELSE

MOVE “NOT A” TO MSG-PR

END-IF

AND/OR - different YES and NO processing

IF INVCD = “A”

N

Y

IF AMT-FST > 500

MOVE “NOT A”

TO MSG-PR

Y

N

MOVE “OK - FST”

TO MSG-PR

IF AMT-SND > 200

N

Y

MOVE “ERROR”

TO MSG-PR

MOVE “OK- SND”

TO MSG-PR

AND/OR - different processing for everything

SIMPLE AND/OR IF

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OK - FST” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OK - SND” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

ELSE

MOVE “NOT A” TO MSG-PR.

SIMPLE AND/OR IF

IF INVCD = “A”

IF AMT-FST > 500

MOVE “OK - FST” TO MSG-PR

ELSE

IF AMT-SND > 200

MOVE “OK - SND” TO MSG-PR

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

END-IF

ELSE

MOVE “ERROR” TO MSG-PR

END-IF

CONTINUE

IF INVCD = “A”

N

Y

IF AMT > 5000

IF INVCD = “A”

IF AMT > 5000

PERFORM B-300-CALC

ELSE

CONTINUE

END-IF

MOVE “OKAY” TO MSG-PR

END-IF

Y

N

CONTINUE

PERFORM

B-300-CALC

With this code, when CONTINUE is encountered, control goes to the MOVE “OKAY” command. IF NEXT SENTENCE was used control would drop entirely out of the IF.

MOVE “OKAY”

TO MSG-PR