C# 中的邏輯運算符

PHPz
發布: 2024-09-03 15:08:17
原創
961 人瀏覽過

電腦程式設計中的邏輯運算子是用於根據某些條件的值來控製程式流程的運算子。操作數可以被視為導致 true 或 false 的條件值。雖然運算符被稱為邏輯運算符,但它們的使用並不限於布林值,而是可以與所有類型一起使用。邏輯運算子的主要功能是將關係語句轉換為條件值。在 C# 中,邏輯運算子用於對兩個或多個運算元執行邏輯運算。

這些邏輯運算包括邏輯與、邏輯或、邏輯非。邏輯運算子可以用作邏輯條件運算符以及關係條件運算符,並且操作數值的使用(就其作為物理值或布林值的存在而言)取決於邏輯運算符在運算中的使用。邏輯運算子是邏輯 GATE 運算的基本翻譯,並遵循邏輯 GATE 對應項的精確邏輯。

C# 中的邏輯運算子

下面詳細解釋c#中的前四種邏輯運算符:

1.邏輯與運算子

如果兩個操作數的值都為true,則邏輯AND 運算子的計算結果為true,即,當且僅當運算中使用的操作數本身計算結果為true 時,邏輯AND 運算的值才等於true。 AND 的邏輯運算是透過使用兩個 && 來表示。

Name Description Syntax Symbol
Logical AND The logical operation yields true if and only if the value of the operand in non zero a && b &&

真值表:

Logical AND
A B OUTPUT
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE
邏輯與

A

B
Name Description Syntax Symbol
Logical OR The logical operation yields true if the value of any of its operand in non zero. a || b ||

輸出

正確 正確 正確 正確 錯誤 錯誤 錯誤 正確 錯誤 錯誤 錯誤 錯誤 表> 2.邏輯或運算子
Logical OR
A B OUTPUT
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
如果運算中使用的運算元的任何值是非零實體,則邏輯運算子將轉換為計算結果為非零的語句。用於邏輯運算的符號表示為||。 名稱 描述 語法 符號 邏輯或 如果任何操作數的值非零,則邏輯運算結果為 true。 一個|| b || 表> 真值表: 邏輯或 A B 輸出 正確 正確 正確 正確 錯誤 正確 錯誤 正確 正確 錯誤 錯誤 錯誤 表>

3.邏輯非運算子

邏輯非的物理存在是基於否定原理。顧名思義,邏輯 NOT 運算子用於將主運算元求反為其邏輯相反值。

Name Description Syntax Symbol
Logical NOT The logical operation yields true if the value of the operand is zero or False. !a !

4.邏輯異或(邏輯異或)

當且僅當運算中兩個運算元的值不相等時,邏輯 XOR 條件才會計算為 true。這由符號^表示。這廣泛用於需要基於操作數相等進行隔離的情況。

Name Description Syntax Symbol
Logical Exclusive OR The logical operation yields true if the value of both of its operands is unequal. a^ b ^

真值表:

Logical XOR
A B OUTPUT
TRUE TRUE FALSE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
邏輯異或

A

B

輸出

正確 正確 錯誤 正確 錯誤 正確 錯誤 正確 正確 錯誤 錯誤 錯誤 表> C# 中邏輯運算子的範例
讓我們用下面的例子來說明上述邏輯。

邏輯與範例

以下是 C# 中邏輯 AND 運算子的範例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int X = 11, Y = 10;
bool logicalAND;
// AND operator
logicalAND = (X <= Y) && (X > 10);
Console.WriteLine(" Result of AND Operation : " + logicalAND);
Console.WriteLine("Press enter to Exit");
Console.ReadLine();
}
}
}
登入後複製

範例#1

代碼:

C# 中的邏輯運算符

輸出:

如果我們改變y的值,AND運算的真實值就會出現。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 11, y = 20;
bool logicalAND;
logicalAND = (x <= y) && (x > 10);
Console.WriteLine(" Result of AND Operation : " + logicalAND);
Console.WriteLine("Press enter to Exit..");
Console.ReadLine();
}
}
}
登入後複製

範例#2

代碼:

C# 中的邏輯運算符

輸出:

邏輯或的範例

以下是 C# 中邏輯 OR 運算子的範例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int X = 11, Y = 20;
bool logicalOR;
// AND operator
logicalOR = (X >= Y) || (X < 8);
Console.WriteLine(" Result of OR Operation : " + logicalOR);
Console.WriteLine("Press enter to Exit");
Console.ReadLine();
}
}
}
登入後複製

範例#1

代碼:

C# 中的邏輯運算符

輸出:

這將計算為 False,因為兩個邏輯運算元的計算結果都是 false。為了證明 OR 運算子的真實出現,讓我們將 X 的值改為 21,大於 Y。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int X = 21, Y = 20;
bool logicalOR;
// AND operator
logicalOR = (X >= Y) || (X < 8);
Console.WriteLine(" Result of OR Operation : " + logicalOR);
Console.WriteLine("Press enter to Exit");
Console.ReadLine();
}
}
}
登入後複製

範例#2

代碼:

C# 中的邏輯運算符

輸出:

邏輯 NOT 範例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool a = true, logicalNOT;
logicalNOT = !a;
Console.WriteLine(" Result of NOT Operation : " + logicalNOT);
Console.WriteLine("Press enter to Exit");
Console.ReadLine();
}
}
}
登入後複製

以下是 C# 中邏輯 NOT 運算子的範例。

代碼:

C# 中的邏輯運算符

輸出:

邏輯異或範例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int X = 21, Y = 22;
bool logicalXOR;
logicalXOR = (X > Y) ^ (X < 22);
Console.WriteLine(" Result of XOR Operation : " + logicalXOR);
Console.WriteLine("Press enter to Exit");
Console.ReadLine();
}
}
}
登入後複製

以下是 C# 中邏輯異或運算子的範例。

代碼:

C# 中的邏輯運算符

輸出:

當 X 的值 > 時,這將產生 true Y 運算元為假,X 結論 透過上面的例子,我們已經了解了C#中的各種邏輯運算符。邏輯運算子的主要用途可以在決策流程圖中找到,它們用於根據操作數的狀態進行條件決策。 AND、OR、NOT 運算符是條件評估中使用的傳統邏輯運算符,而 XOR 則是現代運算符。術語邏輯運算子的出現是因為涉及邏輯運算子的所有運算的輸出都是布林值,即 true 或 false。

以上是C# 中的邏輯運算符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!