Mocka : IF ELSIF handling
While working on my PLOV compiler I got curious: how is a multicondition IF statement taken care of? Have a look at this source:
MODULE kkk;
IMPORT InOut;
VAR w : CARDINAL;
BEGIN
w := 1;
IF w = 1 THEN InOut.WriteString ("w = 1"); InOut.WriteLn;
ELSIF w < 2 THEN InOut.WriteString ("w < 2"); InOut.WriteLn;
ELSIF w = 1 THEN InOut.WriteString ("w still 1"); InOut.WriteLn;
ELSIF w = 2 THEN InOut.WriteString ("w = 2"); InOut.WriteLn;
ELSE
InOut.WriteString ("the ELSE claus")
END;
InOut.WriteLn
END kkk.
We initialize w to '1'. Due to the IF line, "w = 1" will be printed. But how about the other conditions? The
first 2 ELSIF criteria also have matching conditions. So, what will be the outcome? I won't tease you any
longer. This is the output:
jan@beryllium:~/modula/Plov$ kkk w = 1 jan@beryllium:~/modula/Plov$After the first matching condition, the appropriate action is taken and all other ELSIF conditions are skipped. This is logical, but it si also nice to know. And, in fact, an IF/ELSIF construction in fact is the same as an IF cond1 OR cond2 ... construction.
| Source 1 | Identical to |
|---|---|
IF a < 9 AND a > 2 THEN action1 END
|
IF a < 9 THEN
IF a > 2 THEN action 1 END
END
|
IF a = 9 OR a = 2 THEN action2 END
|
IF a = 9 THEN action2
ELSIF a = 2 THEN action2
END
|
Page created 21 September 2008 and
Page equipped with FroogleBuster technology