Example to test all conditions of Logical Operators in C#

using System;
 
public class ExampleLogicalOperators
{
   public static void Main(string[] args)
   {
      // create truth table for && (conditional AND) operator
      Console.WriteLine( "{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
         "Conditional AND (&&)", "false && false", ( false && false ),
         "false && true", ( false && true ),
         "true && false", ( true && false ),
         "true && true", ( true && true ) );
 
      // create truth table for || (conditional OR) operator
      Console.WriteLine( "{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
         "Conditional OR (||)", "false || false", ( false || false ),
         "false || true", ( false || true ),
         "true || false", ( true || false ),
         "true || true", ( true || true ) );
 
      // create truth table for & (boolean logical AND) operator
      Console.WriteLine( "{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
         "Boolean logical AND (&)", "false & false", ( false & false ),
         "false & true", ( false & true ),
         "true & false", ( true & false ),
         "true & true", ( true & true ) );
 
      // create truth table for | (boolean logical inclusive OR) operator
      Console.WriteLine( "{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
         "Boolean logical inclusive OR (|)",
         "false | false", ( false | false ),
         "false | true", ( false | true ),
         "true | false", ( true | false ),
         "true | true", ( true | true ) );
 
      // create truth table for ^ (boolean logical exclusive OR) operator
      Console.WriteLine( "{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
         "Boolean logical exclusive OR (^)",
         "false ^ false", ( false ^ false ),
         "false ^ true", ( false ^ true ),
         "true ^ false", ( true ^ false ),
         "true ^ true", ( true ^ true ) );
 
      // create truth table for ! (logical negation) operator
      Console.WriteLine( "{0}\n{1}: {2}\n{3}: {4}",
         "Logical negation (!)", "!false", ( !false ),
         "!true", ( !true ) );
   } // end Main
} // end class ExampleLogicalOperators

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.