在 C# 中,“且”和“或”是逻辑运算符,用于连接两个或多个条件,产生一个布尔值(true 或 false)。这些运算符在条件语句(如 if)和循环(如 while)中非常常见。
逻辑与(AND) - 用符号 && 表示。如果两个操作数都为 true,则结果为 true;如果任一操作数为 false,则结果为 false。它只有在所有条件都满足时才返回 true。
示例:
if (a > 0 && b > 0)
{
Console.WriteLine("Both a and b are positive numbers.");
}
逻辑或(OR) - 用符号 || 表示。如果两个操作数中至少有一个为 true,则结果为 true;只有当所有操作数都为 false 时,结果才为 false。它只要有一个条件满足就返回 true。
示例:
if (a > 0 || b > 0)
{
Console.WriteLine("At least one of a or b is a positive number.");
}










