# IF statements in COBOL by ewghwehws

VIEWS: 21 PAGES: 20

• pg 1
```									      IF Statements
flowcharts and pseudo code
Simple IF - processing on Y only

N         IF AMT > 5000   Y

AMT x 10
= ANS

if amt > 5000
ans = amt * 10
end if
Simple IF - processing on Y or N

N      IF AMT > 5000     Y

ANS = AMT x 5                            ANS= AMT x 10

In this example, if the AMT is greater
than 5000 then we want to multiply
AMT by 10 and store the answer in
if amt > 5000
ans = amt * 10                          ANS. If the AMT is not greater than
else                                        5000 then we want to multiply AMT
ans = amt * 5                           by 5 and store the answer in ANS.
end if
AND relationship

N         IF INVCD = “A”              Y

N                                      Y
IF AMT > 5000

MOVE “OKAY”
TO MSG

if invcd = “A”
if amt > 5000
move “OKAY to msg                    Both statements must be true for
end if                                   the msg to receive the word
end if                                      OKAY. If either statement is
false, no processing is done.
AND relationship

N         IF INVCD = “A”          Y

N                            Y
IF AMT > 5000

MOVE “OKAY”
TO MSG

if invcd = “A”
if amt > 5000                            if invcd = “A” and amt > 5000
move “OKAY to msg                       move “OKAY” t0 msg
end if                                   end if
end if
AND relationship

N        IF INVCD = “A”          Y

N                            Y
IF AMT > 5000
MOVE
“PROBLEM”
TO MSG
MOVE
“PROBLEM”                            MOVE “OKAY”
TO MSG                               TO MSG

if invcd = “A”
if amt > 5000                           if invcd = “A” and amt > 5000
move “OKAY to msg                      move “OKAY” to msg
else                                    else
move “PROBLEM” to msg                  move “PROBLEM” to msg
end if                                  end if
else
move “PROBLEM” to msg
end if
AND relationship

N        IF INVCD = “A”    Y

N                   Y
IF AMT > 5000

MOVE
“PROBLEM”                   MOVE “OKAY”
TO MSG                      TO MSG

if invcd = “A”
if amt > 5000
move “OKAY to msg
else
move “PROBLEM” to msg
end if
end if
AND relationship

N       IF INVCD = “A”    Y

N                   Y
IF AMT > 5000
MOVE
TO MSG
MOVE
“PROBLEM”                   MOVE “OKAY”
TO MSG                      TO MSG

if invcd = “A”
if amt > 5000
move “OKAY to msg
else
move “PROBLEM” to msg
end if
else
end if
OR relationship
N                         Y
IF INVCD = “A”

MOVE “OKAY”
TO MSG

N                            Y
IF AMT > 5000

MOVE “OKAY”
TO MSG

if invcd = “A”
move “OKAY” to msg
One or the other of the if                    else
if amt > 5000
questions must be true
move “OKAY” to msg
for OKAY to be moved                             end if
to msg.                                       end if
OR relationship
N                               Y
IF INVCD = “A”

MOVE “OKAY”
TO MSG

N                             Y
IF AMT > 5000

MOVE “OKAY”
TO MSG

if invcd = “A” or amt > 5000                    if invcd = “A”
move “OKAY to msg                               move “OKAY” to msg
end if                                          else
if amt > 5000
move “OKAY” to msg
end if
end if
OR relationship
N                      Y
IF INVCD = “A”

MOVE “OKAY”
TO MSG

N                         Y
IF AMT > 5000

MOVE
“PROBLEM”                        MOVE “OKAY”
TO MSG                           TO MSG

if invcd = “A”
move “OKAY” to msg
else
if amt > 5000
move “OKAY” to msg
else
move “PROBLEM” to msg
end if
end if
OR relationship
N                         Y
IF INVCD = “A”

MOVE “OKAY”
TO MSG

N                         Y
IF AMT > 5000

MOVE
“PROBLEM”                        MOVE “OKAY”
TO MSG                           TO MSG

if invcd = “A” or amt > 5000              if invcd = “A”
move “OKAY to msg                         move “OKAY” to msg
else                                      else
move “PROBLEM” to msg                     if amt > 5000
end if                                           move “OKAY” to msg
else
move “PROBLEM” to msg
end if
end if
OR relationship
N                       Y
IF INVCD = “A”

MOVE
“CODE - OKAY”
TO MSG
N                         Y
IF AMT > 5000

MOVE
“PROBLEM”                           MOVE
TO MSG                         “AMT - OKAY”
TO MSG

if invcd = “A”
move “CODE - OKAY” to msg
else
if amt > 5000
move “AMT - OKAY” to msg
else
move “PROBLEM” to msg
end if
end if
cond1 AND (cond 2 OR cond3)                       if invcd = “A”
if amtfst > 500
move “OKAY” to msg
else
if amtsnd > 200
N       IF INVCD = “A”         Y                       move “OKAY” to msg
end if
end if
end if

N                        Y
IF AMTFST > 500

MOVE “OKAY”
TO MSG
N                      Y
IF AMTSND > 200

MOVE “OKAY”
TO MSG
cond1 AND (cond 2 OR cond3)
if invcd = “A” and (amtfst > 500 or amtsnd > 200)
move “OKAY” to msg
end if
Note the use of parenthesis here. In
N       IF INVCD = “A”         Y              boolean logic AND gets resolved before
OR. That means if we did not have the
parenthesis, it would treat this like invcd =
A and amtfst > 500 or just amtsnd > 200.

N                           Y
IF AMTFST > 500
Continue
below
MOVE “OKAY”
TO MSG
N                      Y
IF AMTSND > 200

We want the logic to be
invcd = A and either amtfst
MOVE “OKAY”                      > 500 or amtsnd >200. To
TO MSG
do this, we need to change
the order of operation so
that the two things in the or
relationship are grouped
together. We do this by
using parenthesis.
cond1 AND (cond 2 OR cond3)                       if invcd = “A”
if amtfst > 500 or amtsnd > 200
move “OKAY” to msg
end if
end if
N       IF INVCD = “A”         Y

N                          Y
IF AMTFST > 500

MOVE “OKAY”
TO MSG
N                      Y
IF AMTSND > 200

MOVE “OKAY”
TO MSG
cond1 AND cond 2 OR cond3         if invcd = “A” and amtfst >500 or amtsnd > 200
move “OKAY” to msg
end if

N         IF INVCD = “A”             Y

N                           Y
IF AMTSND > 200
N                           Y
IF AMTFST > 500
MOVE “OKAY”
TO MSG

MOVE “OKAY”
N                            Y               TO MSG
IF AMTSND > 200

MOVE “OKAY”
TO MSG
cond3 OR cond1 AND cond2
N                             Y
IF AMTSND > 200

N                                Y                             MOVE “OKAY”
IF INVCD = “A”
TO MSG

N                             Y
IF AMTFST > 500

MOVE “OKAY”
TO MSG

if amtsnd > 200 or invcd = “A” and amtfst > 500
move “OKAY” to msg
end if
cond1 AND (cond 2 OR cond3)                       if invcd = “A”
if amtfst > 500
move “>500 - OKAY” to msg
else
if amtsnd > 200
N       IF INVCD = “A”          Y                      move “>200 -OKAY” to msg
else
move “PROBLEM” to msg
end if
end if
end if
N     IF AMTFST > 500        Y

MOVE “
>500 - OKAY”
N                       Y                     TO MSG
IF AMTSND > 200

MOVE                           MOVE
“PROBLEM”                     “>200 -OKAY”
TO MSG                        TO MSG
cond1 AND (cond 2 OR cond3)                           if invcd = “A”
if amtfst > 500
move “>500 - OKAY” to msg
else
if amtsnd > 200
move “>200 -OKAY” to msg
else
N       IF INVCD = “A”          Y                          move “PROBLEM” to msg
end if
end if
MOVE                                               else
“CODE PROBLEM”                                            move “CODE PROBLEM” to msg
TO MSG                                             end if
N     IF AMTFST > 500   Y

MOVE “
>500 - OKAY”
N                       Y                         TO MSG
IF AMTSND > 200

MOVE                           MOVE
“PROBLEM”                     “>200 -OKAY”
TO MSG                        TO MSG

```
To top