C# Logical Operator


Objective

In this chapter you will learn:

What is C# Logical Operator?
How many types of Logical Operators in C sharp?
How to use Logical operators in a program?


The C# Logical Operator also evaluates the values and returns true or false as output. Based on true-false the program behaves dynamically at run time. This operator is widely used with C# programming.

&& Operator :It is pronounced as and operator. It returns true if both or all the condition is true and return false if any of the condition is false.




Examples:


using System;
 
namespace and_operator
{
  class Program
   {
     static void Main(string[] args)
      {
        string name,password;
 
        name="Steven";
        password="Steven123";
 
        // evaluating both expresson and returns true if              all are true.
        if (name == "Steven" && password == "Steven123")
         {
           Console.WriteLine("Login Successful");
         }
        else
         {
           Console.WriteLine("Unauthorised access");
         }
        Console.ReadLine();
      }
   }
}




Output

 

Login Successful



|| Operator:It is pronounced as or operator. It also returns true or false based on condition. If any one of the condition matches then it returns true but if both or all the conditions are false then it returns false.


Examples:


using System;
 
namespace Or_operator
{
  class Program
   {
     static void Main(string[] args)
      {   
        string username, userpassword;
 
        label: //Creating label
 
        Console.Write("\n\nEnter your login name:\t");
        username = Console.ReadLine();
 
        Console.Write("Enter your password:\t");
        userpassword = Console.ReadLine();
 
        if ((username == "Steven" || username == "Clark")               && (userpassword == "Steven Clark"))
         {
           Console.WriteLine("\nLogin Successful.");
         }
        else
         {
           Console.WriteLine("\nUnauthorised Access.                   Aborting...");
         }
 
        Console.Write("\n\nPress Y or y for continue.:\t");
        char ans = Convert.ToChar(Console.ReadLine());

        if (ans == 'Y' || ans == 'y')
         {
           goto label; //goto label
         }
        Console.WriteLine("Press  Enter for Aborting...");
        Console.ReadLine();
      }
   }
}


After debugging this code, you can login by two different names as Steven and Clark. The password would be same with both username as Steven Clark.

Output

 

Enter your login name :  Steven
Enter your password :    Steven Clark

Login Successful.


Press Y or y for continue...:    y


Enter your login name :   Clark
Enter your password :     Steven Clark

Login Successful.


Press Y or y for continue...:    y


Enter your login name :   Mark
Enter your password :     Steven Clark

Unauthorised Access. Aborting...


Press Y or y for continue...:    n
Press Enter for Aborting...



! Operator:It is pronounced as not operator. It returns true if expression is false. The following demonstration will clear the concept of not operator.


Examples:


using System;
 
namespace Not_Operator
{
  class Program
   {
     static void Main(string[] args)
      {
        string username, password;
 
        Console.Write("Enter user name:\t");
        username = Console.ReadLine();
        Console.Write("Enter Password:\t");
        password = Console.ReadLine();
 
        if (!(username == "Steven" && password == "Clark"))
         {
           Console.WriteLine("\nLogin Successful");
         }
        else
         {
           Console.WriteLine("\nUnauthorised Access.                   Aborting...");
         }
        Console.ReadLine();
      }
   }
}


In the above example if you will enter the username as Steven and Password as Clark then it will deny you to login because it is evaluated by not (!) operator that returns false if the condition matches.

Output

 

Enter user name :    Steven
Enter Password :  Clark

Unauthorised Access. Aborting...



^ Operator: It is pronounced as xor operator. It returns false if the following condition matches:

(i) if both or all the expression returns true.
(ii) If both or all the expression returns false.


Examples:


using System;
 
namespace xor_operator
{
  class Program
   {
     static void Main(string[] args)
      {
        string name, password;
 
        name = "Steven";
        password = "Clark";
 
        //it returns false because both expression match.
        if ((name == "Steven") ^ (password == "Clark"))
         {
           Console.WriteLine("Acess granted...");
         }
        else
         {
           Console.WriteLine("Access Denied. Aborting...");
         }
        Console.ReadLine();
      }
   }
}


In the above example, the both expression returns true, so the xor operator returns false and print “Access Denied. Aborting…”

Output

 

Access Denied. Aborting...




Summary

In this chapter you learned about different type of C# logical operator as && (and) operator, || (or) operator, ! (not) operator and ^ (xor) operator. You also learned how to use these operators in a program. In next chapter you will see some programming examples of C# operators.



     

Previous Page Next Page