PreviousUpNext

5.2.6  Comparisons and Conditionals

Mythryl comparison operators are nearly the same as in C.

The Mythryl if statement is a bit different from that of C. In particular, the if statement ends with a mandatory matching fi keyword. This means that you do not need curly braces around a block of conditional code in Mythryl, which makes the code a bit more concise:

    #!/usr/bin/mythryl

    a = 10;
    b = 3;

    if   (a == b)  printf "a == b is TRUE.\n";
    else           printf "a == b is FALSE.\n";
    fi;

    if   (a <  b)  printf "a <  b is TRUE.\n";
    elif (a >  b)  printf "a >  b is TRUE.\n";
    elif (a == b)  printf "a == b is TRUE.\n";
    else           printf "a and b are INCOMPARABLE.\n";
    fi;

    if   (not (a <= b))   printf "a <= b is FALSE\n";
    fi;

    if (a >= b)  printf "a >= b is TRUE\n";
    else         printf "a >= b is FALSE\n";
    fi;

    if (a != b)  printf "a != b is TRUE\n";
                 printf "a != b is TRUE\n";
    else
                 printf "a != b is FALSE\n";
                 printf "a != b is FALSE\n";
    fi;

When run this produces

    linux$ ./my-script
    a == b is FALSE.
    a >  b is TRUE.
    a <= b is FALSE.
    a >= b is TRUE.
    a != b is TRUE.
    a != b is TRUE.
    linux$

Mythryl also supports a C-style trinary conditional expression. Unlike C, the regular Mythryl if statement may also be used as a value-yielding expression. (In fact, as we shall see, just about every Mythryl statement may be used as a value-yielding expression.)

    #!/usr/bin/mythryl

    a = 10;
    b = 3;

    printf "a == b is %s\n"   (a == b  ?? "TRUE" :: "FALSE");

    printf "a != b is %s\n"   if (a != b) "TRUE"; else "FALSE"; fi;

When run this produces

    linux$ ./my-script
    a == b is FALSE
    a != b is TRUE
    linux$

The final major Mythryl conditional statement is the case statement. In its simplest form, it may be used much like the C switch statement:

    #!/usr/bin/mythryl

    a = 3;

    case a
     1 => print "I\n";
     2 => print "II\n";
     3 => print "III\n";
     4 => print "IV\n";
     5 => print "V\n";
     6 => print "VI\n";
     7 => print "VII\n";
     8 => print "VIII\n";
     9 => print "IX\n";
    10 => print "X\n";
     _ => print "Gee, I dunno!\n";
    esac;

Here the underline serves as a match-anything wildcard. We will have more to say about this later. For now, just think of it as an otherwise clause.

When run the above produces

    linux$ ./my-script
    III
    linux$

Once again, unlike the C switch statement, the Mythryl case statement may also be used as an expression, for its value:

    #!/usr/bin/mythryl

    a = 3;

    print    case a
             1 => "I\n";
             2 => "II\n";
             3 => "III\n";
             4 => "IV\n";
             5 => "V\n";
             6 => "VI\n";
             7 => "VII\n";
             8 => "VIII\n";
             9 => "IX\n";
            10 => "X\n";
             _ => "Gee, I dunno!\n";
            esac;

This is prettier because now we only have to type the print in once.

When run, the above version produces exactly the same results as the original version:

    linux$ ./my-script
    III
    linux$

If you wish to have more than one statement per alternative in the Mythryl case statement you must enclose them in curly braces. Here is an example which combines the previous two examples: It both prints a value within the case statement and also returns a value. (The last statement in a curly-brace code block is the value of that block.)

    #!/usr/bin/mythryl

    a = 3;

    print    case a
             1 => { print "I\n";             "I\n";             };
             2 => { print "II\n";            "II\n";            };
             3 => { print "III\n";           "III\n";           };
             4 => { print "IV\n";            "IV\n";            };
             5 => { print "V\n";             "V\n";             };
             6 => { print "VI\n";            "VI\n";            };
             7 => { print "VII\n";           "VII\n";           };
             8 => { print "VIII\n";          "VIII\n";          };
             9 => { print "IX\n";            "IX\n";            };
            10 => { print "X\n";             "X\n";             };
             _ => { print "Gee, I dunno!\n"; "Gee, I dunno!\n"; };
            esac;

When run, this prints the value twice:

    linux$ ./my-script
    III
    III
    linux$

We shall have more to say about case statements later, but that is enough to get you started.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext