prev | toc | next
 

3.2 Overview: MUF (cont'd)

Conditional Statements:

A conditional statement causes the program to do something only if a certain condition is true. The main form of a conditional statement in MUF is the IF-THEN statement.

IF-THEN works a bit differently in MUF than in some other programming languages. In BASIC, for example, the meaning is something like `IF A is true, THEN go do B'. In MUF, the meaning is more like `IF A is true, do this bit here... B. THEN do all this other stuff, the rest of the program.'

'True', as far as MUF is concerned, means `any value other than the integer 0, or the floating point number 0.0000, or a null string ( "" ), or the dbref for "nothing", which is #-1'.

        0 <---- False
0.0000 <---- False
"" <---- False
#-1 <---- False
 
1 <---- True
3.14159  <---- True
"Hiya!" <---- True
#123 <---- True

An IF checks (and uses up) the top value on the stack. When this value is true, any code between the IF and its matching THEN will execute (every IF must have a matching THEN). When the value is false, the program skips over everything until it gets to the matching THEN, and resumes execution at that point. Here's an example...

====================================
: main
   
    random 1000000 > if
        me @ "Yes, the number is greater than one million." notify
    then
   
    me @ "OK, we're done with the IF-THEN stuff." notify
;
====================================

RANDOM puts a random number between one and the largest integer it can generate (about 2.1 billion) on the stack. The > greater than operator tests the two numbers right before it, and returns true (that is, puts a 1 on the stack) if the first number is greater than the second. If the first number is less than the second, it returns false (it puts a 0 on the stack). The two numbers being compared are used up in the process.

So, when our little program gets to IF in the first line, there will either be a 1 or a 0 on the stack. If the value on the stack is 1 (which it will be the vast majority of the time), the IF test will be true, and the next line will execute: the program will notify the user with `Yes, the number is greater than one million.' Then it will continue past the THEN and do the next line, notifying the user `OK, we're done with the IF-THEN stuff.' If the random number were less than than one million, the IF test would be false, and the program would skip straight to the last line.

You can also specify what should happen when the IF test is false by placing an ELSE between the IF and the THEN. The following version will notify whether the random number is greater or less than one million.

====================================
: main

    random 1000000 > if
        me @ "Yes, the number is greater than one million." notify
    else
        me @ "No, the number is less than one million." notify
    then
   
    me @ "OK, we're done with the IF-ELSE-THEN stuff." notify
;
====================================

Intenting one level for each condition, as in these examples, is a common and helpful convention.

prev | toc | topnext