PreviousUp

10.7.2  if else fi

The Mythryl if-else-fi is fairly conventional. Unlike in C, it is an expression which returns either the value of its then or else branch, whichever is selected by the controlling Boolean expression. To keep this well-typed, this means that both branches must evaluate to values of the same type.

For conciseness, the then and else branches of the Mythryl if expression are implicit code blocks: each may contain an arbitrary sequence of statements, and takes on the value of the final statement in the sequence:

    linux$ my

    eval:  if (1 == 1) "red"; else "green"; fi;

    "red"

    eval:  if (1 == 2) "red"; else "green"; fi;

    "green"

The conditional expression must be parenthesized unless it is a single variable, or a variable with a close-binding prefix, postfix or circumfix operator, typically a dereference.

The else clause may be dropped, in which case it takes on a default value of Void, meaning that the then clause must also have a Void value:

    linux$ cat my-script 
    #!/usr/bin/mythryl

    if TRUE
       print "True.\n";
    fi;

    if FALSE
       print "False.\n";
    fi;

    linux$ ./my-script
    True.

As usual, elif may be used to construct a chain of tests and actions:

    linux$ cat my-script 
    #!/usr/bin/mythryl

    x = 2;

    if   (x == 1)  print "One.\n";
    elif (x == 2)  print "Two.\n";
    elif (x == 3)  print "Three.\n";
    else           print "Many.\n";
    fi;

    linux$ ./my-script
    Two.

Comments and suggestions to: bugs@mythryl.org

PreviousUp