if statements in cobol l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
IF statements in COBOL PowerPoint Presentation
Download Presentation
IF statements in COBOL

Loading in 2 Seconds...

play fullscreen
1 / 30

IF statements in COBOL - PowerPoint PPT Presentation


  • 189 Views
  • Uploaded on

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.

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

PowerPoint Slideshow about 'IF statements in COBOL' - benjamin


An Image/Link below is provided (as is) to download presentation

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

IF statements in COBOL

Please open the speaker notes - they contain additional information!

slide2

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.

slide3

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 <=
slide4

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

store the answer in ANS-WS.

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.

slide5

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.

slide6

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.

slide7

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

slide8

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

slide9

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.

slide10

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.

slide11

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.

slide12

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

slide13

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.

slide14

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

slide15

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

slide16

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

slide17

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.

slide18

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

slide19

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.

slide20

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.

slide21

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

slide22

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
slide23

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

slide24

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

slide25

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

slide26

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

slide27

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

slide28

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

slide29

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

slide30

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