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:
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:
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:
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...
&& 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();
}
}
}
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());
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();
}
}
}
{
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();
}
}
}
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
Post a Comment