Skip to main content

C# Logical Operator

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="Aav";
        password="Aav123";

        // evaluating both expresson and returns true if all are true.
        if (name == "Aav" && password == "Aav123")
         {
           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 == "AAV" || username == "Kumar")&& (userpassword == "AAV Kumar"))
         {
           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 : AAV
Enter your password :    Kumar

Login Successful.


Press Y or y for continue...:    y


Enter your login name :  Kumar
Enter your password :     AAV Kumar

Login Successful.


Press Y or y for continue...:    y


Enter your login name :  Mukesh
Enter your password :     AAV Kumar

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 == "Aav" && password == "Kumar"))
         {
           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 :   Aav
Enter Password : Kumar

Unauthorised Access. Aborting...




Comments

Popular posts from this blog

C# Array

Array is a collection of variable of same data type. If you have declare 1000 integer variable, then you can declare an integer type array of 1000 size. The value of array can be accessed using index position of array. The first index position of array is zero. In C#, there two types of array: Single Dimensional Array and Multi Dimensional Array. You can use both type of array easily and can access its element using loop constructs or index position.

Structure (C#)

Structure is the value type data type that can contain variables, methods, properties, events and so on. It simplifies the program and enhance performance of code in C# programming. The structure encapsulate small group of related variables inside a single user-defined data type. It improves speed and memory usage and also enhances performance and clarity of your code. How to use structure in C#? It is very simple to use structure in C#. The following programming example will show you to how to create and use structure in C# programming. Programming Example of Structure (C#) using System; namespace Structure {    class Program    {      // creating three different variable in single structure       struct book        {          public string bookname;          public int price;          public stri...