Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ELSE END-IF 2

Status
Not open for further replies.

claudeb

Programmer
Nov 23, 2000
140
CA
hi , when ever i use if, else and end-if; every thing goes wrong. i don't think i understand end-if.
could you explain and give some examples.
thanks
 
I'm a bit rusty but I think you have the syntax wrong. Try END IF or ENDIF without the dash.

IF <condition>
<stmt>
ELSEIF <condition>
<stmt>
ELSE
<stmt>
ENDIF
 
example of a selection. The corresponding IF and END-IF are on the same column.
Code:
IF SEX = &quot;FEMALE&quot; 
   IF INCOME > 50000
       IF INCOME > 100000
           DISPLAY &quot;FEMALE AND INCOME > 100000&quot;
       ELSE
           DISPLAY &quot;FEMALE AND INCOME > 50000&quot;
       END-IF
       IF NUMBER-OF-KIDS = 0 
           DISPLAY &quot;  AND SHE HAS NO KIDS!&quot;
       END-IF
   ELSE
       DISPLAY &quot;FEMALE NOT SELECTED&quot;
   END-IF
ELSE
   PRINT &quot;---> NOT FEMALE&quot;
END-IF

 
now i see,
i put the first end-if before the first else
thanks
 
Hi Claud,

You didn't give us a &quot;ferinstance&quot; so I face the possibility of telling you something you already know. My apologies in advance; but here goes:

The general form is
Code:
       IF cond1
          stmts
       ELSE  
          stmts
       END-IF

where,
   cond1 - can be any arithmetic or logical comparison, or a condition name (88 level);
   stmts - can be any single or group of valid COBOL stmts, including other IF stmts.

Remember periods (.) cannot appear within an IF stmt. When using END-IF they should be avoided, although they can be useful in some circumstances.

If using nested IF stmts be sure that each IF has been terminated by its associated END-IF (work from the inside out). 

Crox's post is a good example of a complex IF stmt, covering most, if not all of what you can do in one.

Another aspect of complex condition testing is mixing IFs and EVALUATEs. I find it helps to make the code a bit more &quot;readable&quot;:

        EVALUATE TRUE   ALSO  TRUE
        WHEN AGE-IS-UNDER-30 ALSO SEX-IS-MALE
             IF INCOME > 50000
                PERFORM 1000-SPAM-HIM
             END-IF 
        WHEN STATE = NJ      ALSO NO-SPAM-SET
             PERFORM 1000-SPAM-HIM 100 TIMES
        END-EVALUATE

Hope this helps, Jack.
 
EVALUATE TRUE ALSO TRUE ???
i am afraid i don't understand this.
you wouldn't believe how much the &quot;esoteric&quot; answer (to one of my questions) helped !
thanks for the time you take to answer.
 
Hi Claude,

Sorry, you probably didn't get to this part yet; I hope I haven't given away the plot.

An EVAL can be used as a replacement for an IF stmt. It's supposed to present complex conditional solutions in a more &quot;readable&quot; form.

they can be coded:

Code:
        EVAL data-name
        EVAL TRUE
        EVAL FALSE
        EVAL data-name ALSO TRUE ALSO FALSE.....
        or in any combination thereof

The EVAL (use the full word) is followed by one or more WHEN stmts followed by an END-EVAL. The ALSO is treated as a logical AND; the OR I'll show later. Let's take that last EVAL:
        EVAL WS-AGE     ALSO   TRUE        ALSO    FALSE   
        WHEN 18 THRU 25 ALSO  WS-WT > 200  ALSO    MALE  
             PERFORM 1000-ENROLL-IN-FBALL-PGM
        END-EVAL

TRANSLATION: Enroll all women greater than 200 lbs between the ages of 18 and 25 in the football program.

The OR situation:

EVAL TRUE ALSO TRUE
WHEN WS-AGE < 18 ALSO WS-AGE > 25
WHEN WS-WT < 200 ALSO ANY
WHEN NOT MALE ALSO ANY
PERFORM 2000-EXPELL
END-EVAL

TRANSLATION: Expell all females under 200 lbs and not between 18 and 25.

Note that each condition is tested in turn; each case is rejected as soon as it fails a condition.


That should give you a little insight into the EVAL. It's not a complete treatment, but it's a start.

Jack



 
Gotta correct Slade's last answer. Multiple WHENs in an EVALUATE are ORed, not ANDed. In his example all Females will be expelled as will anyone under 200 pounds and everyone not between 18 and 25.

Glenn
Brainbench MVP for COBOL II
 
Hi Glenn,

You're right, of course. I don't where I got that clap-trap; I must have had a brain cramp. Very embarrassing!
 
Jack,

it's good to see that you're just as human as the next guy ... ;-)

Regards,
Ronald.
 
Ronald,

If that's the criterium, I'm the poster boy for Humanity.

Regards, Jack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top