Logical Operators in c#

Logical Operators in c#

Logical operators in the C# programming language are used to perform Boolean logical expression operations on user-defined data type variable values ​​and return output values ​​as true or false based on a user-defined end logical condition. Logical operators are essential for creating complex conditional expression statements in C# programs. In a typical C# program, logical operators such as &&, !, ^, etc., can perform program operation logic.

Logical Operators in c#

Details of Logical Operators in the C# Programming Language.

OperatorOperator NameLogical operator DescriptionExampleOutput
&&Logical AND operatorIt Returns true output, when both user define program conditions are truep > 7 && q < 17Result is true
||Logical OR operatorIt Returns true when one user define program conditions is truep < 7 || q > 13Result is true
!Logical NOT operatorIt Reverses the Boolean value operation given in program!(p > 9)Result is false
^Logical XOR operatorIt Returns true output, when exactly one user define program condition is truea > 3 ^ b < 2Result is true

Logical AND (&&) Operator in C#.

The AND logical operator in the C# programming language is used to check the values ​​of two variable parameters. The logical AND operator in a C# program returns the output value true only when both end logical condition expressions defined in the program are defined as true at the same time.

Logical AND (&&) operator example in C#.

using System;

class LogicalAndOP

{

static void Main(string[] args)

{

int p = 7, q = 27;

if (p > 4 && q > 21)

{

Console.WriteLine(“Both user-defined && operator conditions are true.”); // This will execute as true

}

Console.ReadLine(); // This function keeps the console window open for output

}

}

Logical OR (||) operator in C#.

In the C# programming language, the logical AND operator returns a true output value only if at least one user-defined program condition expression in the given logical AND operator is true.

Logical OR (||) operator example in C#.

using System;

class OrLogicalOP

{

static void Main(string[] args)

{

int p = 4, q = 9;

if (p < 6 || q > 13)

{

Console.WriteLine(“At least one OR condition must be true in both variables.”); // This will execute the OR logical condition statement.

}

Console.ReadLine(); // Here it will keep the console window open for operation.

}

}

Logical NOT (!) operator in C#.

The logical NOT (!) operator reverses a Boolean value in a C# program. For example, if the output is true, it returns false. Similarly, if the output is false, it returns true.

Example of the logical NOT (!) operator.

using System;

class LogicalNotOP

{

static void Main(string[] args)

{

bool checkValue = false;

if (!checkValue)

{

Console.WriteLine(“The check value is tested.”); // This will execute the logical NOT operator statement

}

Console.ReadLine(); // Here it keeps the console window open for reading

}

}

Logical XOR (^) operator in C#.

In a C# program, if only one user-added logical condition is true in a user-defined logical expression condition, but both user-defined condition logics are not true, then it displays a true result as output.

Logical XOR (^) operator example.

using System;

class LogicalXorOP

{

static void Main(string[] args)

{

bool logic1 = true;

bool logic2 = false;

if (logic1 ^ logic2)

{

Console.WriteLine(“Here exactly one XOR logical condition expression must be true.”); // This statement will execute here

}

Console.ReadLine(); // This keeps the console window open for reading

}

}

C# Logical Operators Important Notes.

  • Short-circuiting (&& and ||) concept in logical operators in C# programs.
  • && – If the first condition in a C# program is false, it stops evaluating the current program.
  • || – If the first condition in a C# program is true, it stops evaluating the current program.

ShortCircuiting C# Example.

using System;

class ShortCircuiting

{

static void Main(string[] args)

{

int p = 11;

if (p < 17 && TestExpressionReturnsFalse()) // Here this will run TestExpressionReturnsFalse()

{

Console.WriteLine(“it doesn’t execute in the program”);

}

Console.ReadLine(); // Here it keeps the console window open for reading

}

static bool TestExpressionReturnsFalse()

{

Console.WriteLine(“TestExpressionReturnsFalse() is called now”);

return false;

}

}

Combining logical operators in a C# program.

C# programmers can group multiple logical operators together to create complex logical condition expressions in a program.

Combining logical operators C# Example.

using System;

class LogicalAndOROP

{

static void Main(string[] args)

{

int salary = 999;

bool isTested = (salary > 499 && salary < 1999) || (salary==2999);

Console.WriteLine(isTested); // Result – true

Console.ReadLine(); // here it Keeps the console window open for reading

}

}

Leave a Reply