C# - 逻辑运算符
C# 中的逻辑运算符是什么?
C# 逻辑运算符 用于评估多个条件并返回 true 或 false。当需要检查多个条件时,可以使用这些运算符将它们组合起来。这些运算符在编程中可用于决策、循环和数据验证。
逻辑运算符列表
下表列出了 C# 支持的所有逻辑运算符。假设变量 A 的布尔值为 true,变量 B 的布尔值为 false,则 −
运算符 | 符号 | 描述 | 示例 |
---|---|---|---|
逻辑与 | && | 称为逻辑与运算符。如果两个操作数都非零,则条件成立。 | (A && B) 为假。 |
逻辑或 | || | 称为逻辑或运算符。如果两个操作数中有一个非零,则条件成立。 | (A || B) 为真。 |
逻辑非 | ! | 称为逻辑非运算符。用于反转其操作数的逻辑状态。如果条件为真,则逻辑非运算符将返回假。 | !(A && B) 为真。 |
C# 逻辑运算符示例
以下示例演示了 C# 中所有可用的逻辑运算符 -
using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { bool a = true; bool b = true; if (a && b) { Console.WriteLine("Line 1 - Condition is true"); } if (a || b) { Console.WriteLine("Line 2 - Condition is true"); } /* 让我们改变 a 和 b 的值 */ a = false; b = true; if (a && b) { Console.WriteLine("Line 3 - Condition is true"); } else { Console.WriteLine("Line 3 - Condition is not true"); } if (!(a && b)) { Console.WriteLine("Line 4 - Condition is true"); } Console.ReadLine(); } } }
当编译并执行上述代码时,它会产生以下结果 -
Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true
更多逻辑运算符示例
示例 1:在 if 条件中使用逻辑运算符
在此示例中,我们将根据年龄和身份验证检查某人是否允许进入。
using System; class Program { static void Main() { int age = 20; bool hasID = true; if (age >= 18 && hasID) { Console.WriteLine("Allowed to enter."); } else { Console.WriteLine("Entry denied."); } } }
当编译并执行上述代码时,它会产生以下结果 -
Allowed to enter.
示例 2:逻辑与 (&&)
在此示例中,我们根据分数和考试通过状态检查学生是否有资格获得奖学金。
using System; class Program { static void Main() { int score = 85; bool passedExam = true; if (score >= 80 && passedExam) { Console.WriteLine("Eligible for scholarship."); } else { Console.WriteLine("Not eligible."); } } }
当编译并执行上述代码时,它会产生以下结果 -
Eligible for scholarship.
示例 3:逻辑或 (||)
在此示例中,我们将检查某人是否根据 VIP 通行证或普通门票获得访问权限。
using System; class Program { static void Main() { bool hasVIPPass = false; bool hasRegularTicket = true; if (hasVIPPass || hasRegularTicket) { Console.WriteLine("Access granted."); } else { Console.WriteLine("Access denied."); } } }
当编译并执行上述代码时,它会产生以下结果 -
Access granted.
示例 4:逻辑非 (!)
在此示例中,我们检查是否下雨,以决定是否应该外出。
using System; class Program { static void Main() { bool isRaining = false; if (!isRaining) { Console.WriteLine("Go outside!"); } else { Console.WriteLine("Stay indoors."); } } }
在此示例中,我们检查是否下雨,以决定是否应该外出。
Go outside!
用例
以下是一些逻辑运算符的用例:
示例 5:用户登录验证
在此示例中,我们验证用户凭据以允许或拒绝登录。
using System; class Program { static void Main() { string username = "admin"; string password = "pass123"; if (username == "admin" && password == "pass123") { Console.WriteLine("Login successful."); } else { Console.WriteLine("Invalid credentials."); } } }
在此示例中,我们正在检查是否下雨以决定是否应该出门。
Login successful.
示例 6:检查商店折扣资格
在此示例中,我们将根据会员状态或购买金额检查客户是否有资格享受折扣。
using System; class Program { static void Main() { bool isMember = true; double purchaseAmount = 120.0; if (isMember || purchaseAmount > 100) { Console.WriteLine("Discount applied."); } else { Console.WriteLine("No discount available."); } } }
在此示例中,我们检查是否下雨,以决定是否应该外出。
Discount applied.
最佳实践
以下是在 C# 中使用逻辑运算符的最佳实践:
- 您应该使用括号对条件进行分组,例如 ((x && y) || z)。
- 您应该简化表达式,例如,使用 if (!isReady) 而不是 if (isReady == false)。
- 您必须避免冗余检查,例如,使用 if (x) 而不是 if (x == true)。
- 组合条件时,您应该始终测试边缘情况。