next up previous contents index
Next: Special Functions Up: MIDAS Command Language Previous: Local Keywords

Conditional Statements, Branching

    

As in FORTRAN 77 any of the following forms of the IF statement may be used:

IF log_exp command ! command = any MIDAS command
! with at most 4 params.

IF log_exp THEN ! xyz = any logical expression
...
ELSEIF log_exp THEN ! uvw = any logical expression
...
ELSE
...
ENDIF

IF log_exp THEN
...
ENDIF

IF blocks may be nested up to 8 levels deep in a procedure.
The logical expression `log_exp' is of the form:

arg1 op arg2

where arg1, arg2 are either names of keywords (this includes also the names P1, ..., P8) or constants, and op may be any of .EQ., .NE., .GT., .GE., .LT. or .LE. (with the same meaning as in FORTRAN 77). As with symbol substitution, specify single elements of an array and substrings via, e.g., OUTPUTI(7) and IN_B(2:15).
If we do

WRITE/KEYWORD INPUTC beer
WRITE/KEYWORD OUTPUTC wine
WRITE/KEYWORD INPUTR/R/1/3 1.,2.,3.

Then,

INPUTC .EQ. "beer" is TRUE
INPUTC .EQ. "BEER" is also TRUE
INPUTC .EQ. OUTPUTC is FALSE
INPUTC(2:2) .EQ. OUTPUTC(4:4) is TRUE
INPUTR(2) .GT. 5.4 is FALSE

In string comparisons upper and lower case characters are not distinguished in order to guarantee case insensitivity.
Character keywords can only be compared to character keywords or character constants (which are enclosed by double quotes). This can become tricky in conjunction with symbol substitution:

!+
! Example 13a, MIDAS procedure exa13a.prg
!+
DEFINE/PARAM P1 ? N "Enter number: "
DEFINE/MAXPAR 1 ! only one parameter expected
IF P1 .EQ. 1 THEN
WRITE/OUT P1 = 1
ELSE
WRITE/OUT P1 is not = 1
ENDIF

Entering @@ exa13a 1 as well as @@ exa13a 001 will give the expected output message P1 = 1 since the line IF P1 .EQ. 1 THEN has been converted in the first pass by the Monitor to
IF 1 .EQ. 1 THEN or IF 001 .EQ. 1 THEN
and the two integer constants are equal. Now, consider the almost identical procedure exa13b.prg:

!+
! Example 13b, MIDAS procedure exa13b.prg
!+
DEFINE/PARAM P1 ? N "Enter number: "
DEFINE/MAXPAR 1 ! only one parameter expected
IF P1 .EQ. 1 THEN
WRITE/OUT P1 = 1
ELSE
WRITE/OUT P1 is not = 1
ENDIF

Entering @@ exa13b 1 will return the error message invalid IF statement... and abort. Why?
Well, in the IF statement above the contents of the character keyword P1, which is the character `1', is compared to the integer constant 1, an invalid comparison.
We modify the procedure once more:

!+
! Example 13c, MIDAS procedure exa13c.prg
!+
DEFINE/PARAM P1 ? N "Enter number: "
DEFINE/MAXPAR 1 ! only one parameter expected
IF P1 .EQ. "1" THEN
WRITE/OUT P1 = 1
ELSE
WRITE/OUT P1 is not = 1
ENDIF

Now, entering @@ exa13c 1 will work and yield P1 = 1 but @@ exa13c 001 will output P1 is not = 1 since the string "001" is not equal to "1".

As another example let us see, how we can check if a parameter has been entered at all:

!+
! Example 14a, MIDAS procedure exa14a.prg
!+
DEFINE/PARAM P6 + NUMBER "Enter first input number: "
IF P6(1:1) .EQ. "+" THEN
WRITE/KEYWORD INPUTC NONE ! no P6 entered, set INPUTC accordingly
ELSE
WRITE/KEYWORD INPUTI/I/7/1 P6 ! store contents of P6 in INPUTI(7)
WRITE/KEYWORD INPUTC YES !indicate, that INPUTI holds a valid number
ENDIF

However, if we also want to check the limits of the given number we have to use the DEFINE/PARAMETER command again, because testing " +" against a numerical interval would lead to an error:

!+
! Example 14b, MIDAS procedure exa14b.prg
!+
DEFINE/PARAM P6 + NUMBER "Enter first input number: "
IF P6(1:1) .EQ. "+" THEN
WRITE/KEYWORD INPUTC NONE ! no P6 entered, set INPUTC accordingly
ELSE
DEFINE/PARAM P6 + NUMBER "Enter first input number: " 22,1024
WRITE/KEYWORD INPUTI/I/7/1 P6 ! store contents of P6 in INPUTI(7)
WRITE/KEYWORD INPUTC YES !indicate, that INPUTI holds a valid number
ENDIF

Since, in the ELSE branch we know that parameter P6 is given, the default value " +" itself is never tested against the interval [22,1024].

For testing multiple alternatives use the BRANCH command. It has the syntax:
BRANCH variable casea,caseb,...,casez labela,labelb,...,labelz.

!+
! Example 15, MIDAS procedure exa15.prg
!+
DEFINE/PARAMETER P1 ? C "Enter method: "
DEFINE/MAXPAR 1 ! only one parameter expected
!
! Use the first 2 characters of parameter P1 to distinguish the methods BRANCH P1(1:2) AN,DI,HY ANALOG,DIGIT,HYBRID
!
! fall through if no match ...
WRITE/OUT Invalid option - please try again
RETURN
!
ANALOG:
RUN ANALO
RETURN
!
DIGIT:
RUN DIGI
RETURN
!
HYBRID:
RUN HYBRI

Then, @@ exa15 ANALOG will execute the command RUN ANALO and @@ exa15 digital or @@ exa15 di will run the program digi.exe.



next up previous contents index
Next: Special Functions Up: MIDAS Command Language Previous: Local Keywords



Pascal Ballester
Fri Mar 24 17:17:52 MET 1995