The Oberon Programming Language

Truth Tables Example Program


Click below to go directly to a specific section:
Description | Source Code | Sample Run | Program Notes

Description


This code demontsrates the usage of Oberon's Texts module, paremeter passing in functions, loop control commands, if-then-else clauses and boolean variables usage

Source Code

(* This is designed to run Under Oberon V4 *)

MODULE TTable;
IMPORT
    Oberon, Texts; 
VAR
    w: Texts.Writer;
(* Helper function writes T or F depending on the boolean value given *)
PROCEDURE Writebool (x:BOOLEAN);
BEGIN
  IF x=TRUE 
    THEN Texts.WriteString(w,"     T     ")
    ELSE Texts.WriteString(w,"     F     ")
  END;
END Writebool;
(* Performs a logical exclusive or on two boolean parameters *)
PROCEDURE Xor (x:BOOLEAN; y:BOOLEAN): BOOLEAN;
BEGIN
 IF (((x=TRUE) & (y=TRUE)) OR ((x=FALSE) & (y=FALSE))) 
   THEN RETURN FALSE;
   ELSE RETURN TRUE;
 END;
END Xor;
(* Performs a conditional expression operation on two boolean parameters *)
PROCEDURE Cond(x:BOOLEAN; y:BOOLEAN): BOOLEAN;
BEGIN;
 IF (x = TRUE) & (y = FALSE) 
  THEN RETURN FALSE;
  ELSE RETURN TRUE;
 END;
END Cond;

PROCEDURE Write*;
  VAR
      p,q,r,s,t,u:BOOLEAN; (* Truth cases *)
      a,b,c,d,e,f:INTEGER; (* Loop variables *)
 BEGIN
(* Header *)
Texts.WriteString(w,"(((((p -> q) AND r)  OR s) NOR t)  XOR  u)");
Texts.WriteLn(w);
(* Loop for all cases of True and False) 
p:=TRUE; 
 FOR a:=1 TO 2 DO  q:=TRUE;
  FOR b:=1 TO 2 DO r:=TRUE;
   FOR c:=1 TO 2 DO s:=TRUE;
    FOR d:=1 TO 2 DO t:=TRUE;
     FOR e:=1 TO 2 DO u:=TRUE;
      FOR f:=1 TO 2 DO
       Writebool (p);
       Writebool (q);
       Writebool (r);
       Writebool (s);
       Writebool (t);
       Writebool (u);				
       Texts.WriteString (w,"   |     ");
       IF  ( Xor ( ~( ( (Cond(p,q) & r) OR s) OR t), u)) = TRUE
        THEN Texts.WriteString(w, "T") ;
        ELSE Texts.WriteString(w, "F");
       END;
       Texts.WriteLn(w); 
       Texts.Append(Oberon.Log,w.buf);
       u:= FALSE; END;
      t:= FALSE; END;
     s:= FALSE; END;
    r:= FALSE; END;
   q:= FALSE; END;
 r:=FALSE; END;


Sample Run

Once this module is compiled, Executing the command TTable.Write will print this th the system log:
 
   (((((p -> q) AND r)  OR s) NOR t)  XOR  u)
     T          T          T          T          T          T          |    T
     T          T          T          T          T          F          |    F
     T          T          T          T          F          T          |    T
     T          T          T          T          F          F          |    F
     T          T          T          F          T          T          |    T
     T          T          T          F          T          F          |    F
     T          T          T          F          F          T          |    T
     T          T          T          F          F          F          |    F
     T          T          F          T          T          T          |    T
     T          T          F          T          T          F          |    F
     T          T          F          T          F          T          |    T
     T          T          F          T          F          F          |    F
     T          T          F          F          T          T          |    T
     T          T          F          F          T          F          |    F
     T          T          F          F          F          T          |    F
     T          T          F          F          F          F          |    T
     T          F          T          T          T          T          |    T
     T          F          T          T          T          F          |    F
     T          F          T          T          F          T          |    T
     T          F          T          T          F          F          |    F
     T          F          T          F          T          T          |    T
     T          F          T          F          T          F          |    F
     T          F          T          F          F          T          |    F
     T          F          T          F          F          F          |    T
     T          F          F          T          T          T          |    T
     T          F          F          T          T          F          |    F
     T          F          F          T          F          T          |    T
     T          F          F          T          F          F          |    F
     T          F          F          F          T          T          |    T
     T          F          F          F          T          F          |    F
     T          F          F          F          F          T          |    F
     T          F          F          F          F          F          |    T
     T          T          T          T          T          T          |    T
     T          T          T          T          T          F          |    F
     T          T          T          T          F          T          |    T
     T          T          T          T          F          F          |    F
     T          T          T          F          T          T          |    T
     T          T          T          F          T          F          |    F
     T          T          T          F          F          T          |    T
     T          T          T          F          F          F          |    F
     T          T          F          T          T          T          |    T
     T          T          F          T          T          F          |    F
      T          T          F          T          F          T          |    T
     T          T          F          T          F          F          |    F
     T          T          F          F          T          T          |    T
     T          T          F          F          T          F          |    F
     T          T          F          F          F          T          |    F
     T          T          F          F          F          F          |    T
     T          F          T          T          T          T          |    T
     T          F          T          T          T          F          |    F
     T          F          T          T          F          T          |    T
     T          F          T          T          F          F          |    F
     T          F          T          F          T          T          |    T
     T          F          T          F          T          F          |    F
     T          F          T          F          F          T          |    F
     T          F          T          F          F          F          |    T
     T          F          F          T          T          T          |    T
     T          F          F          T          T          F          |    F
     T          F          F          T          F          T          |    T
     T          F          F          T          F          F          |    F
     T          F          F          F          T          T          |    T
     T          F          F          F          T          F          |    F
     T          F          F          F          F          T          |    F
     T          F          F          F          F          F          |    T


Program Notes

This program tested on Oberon V4 Windows 32 bit version. When you create a new file TTable.Mod in Oberon's editor (Edit.Open TTable.Mod), sellect this text now and copy it ito the clipboard (File/Copy), paste it into Hello.Mod by placing the caret in that window and executing Clipboard.Copy command anywhere on the screen. Compiling is done by placing a marker (F1 key) on TTable.Mod window and running Compiler.Compile * in the System.Tool.


[Back] [Home]