C# Logical Operator

LOGIC
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 conditions are true and return false if any of the condition is false.

Example

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 :  Steven
Enter your password :    demopassLogin Successful.Press Y or y for continue. Press N or n for Exit:    yEnter your login name :   Clark
Enter your password :     demopass

Login Successful.

Press Y or y for continue. Press N or n for Exit:    y

Enter your login name :   Mark
Enter your password :     demopass

Unauthorised Access. Aborting…

Press Y or y for continue. Press N or n for Exit:    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 demopass then it will deny you to login because it is evaluated by not (!) operator that returns false if the conditions match.

Output

Enter user name :    Steven
Enter Password :  demopassUnauthorised 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

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.

Write a Reply or Comment

Your email address will not be published.