- C# 基礎教程
- C# - 首頁
- C# - 概述
- C# - 環境
- C# - 程式結構
- C# - 基本語法
- C# - 資料型別
- C# - 型別轉換
- C# - 變數
- C# - 常量
- C# - 運算子
- C# - 決策
- C# - 迴圈
- C# - 封裝
- C# - 方法
- C# - 可空型別
- C# - 陣列
- C# - 字串
- C# - 結構體
- C# - 列舉
- C# - 類
- C# - 繼承
- C# - 多型
- C# - 運算子過載
- C# - 介面
- C# - 名稱空間
- C# - 預處理器指令
- C# - 正則表示式
- C# - 異常處理
- C# - 檔案I/O
C# - 邏輯運算子
下表顯示了 C# 支援的所有邏輯運算子。假設變數A持有布林值 true,變數B持有布林值 false,則:
| 運算子 | 描述 | 示例 |
|---|---|---|
| && | 稱為邏輯與運算子。如果兩個運算元都不為零,則條件為真。 | (A && B) 為假。 |
| || | 稱為邏輯或運算子。如果兩個運算元中的任何一個不為零,則條件為真。 | (A || B) 為真。 |
| ! | 稱為邏輯非運算子。用於反轉其運算元的邏輯狀態。如果條件為真,則邏輯非運算子將使其為假。 | !(A && B) 為真。 |
示例
以下示例演示了 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");
}
/* lets change the value of a and 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
csharp_operators.htm
廣告