C# 三元運算符

PHPz
發布: 2024-09-03 15:09:04
原創
695 人瀏覽過

用來取代條件語句if 和else 進行決策的運算子在C# 中稱為三元運算符,它由三個參數組成,其中第一個參數用於比較,如果比較的結果為true,則傳回第二個參數,如果比較結果為false,則傳回第三個參數,這個運算子可以被認為是使用if-else 語句的最佳化方式。

文法:

Boolean Expression ? First statement : Second statement
登入後複製

上面的語法代表三元運算子。它由三部分組成。語法的第一部分位於 ? 之前。它傳回一個布林值 true 或 false,因為它是一個條件表達式。語法的第二部分位於「?」之前和「:」之後,如果第一部分的條件表達式的結果為 true,則執行該部分。如果第一部分的條件式的結果為 false,則執行「:」語句之後的語法的第三部分。

C# 三元運算子的工作原理

以下是三元運算子的不同範例。

1.使用 If Else

比較兩個值

考慮下面的 C# 程序,使用 if-else 語句比較兩個值。

代碼:

using System;
namespace Project
{
class MainClass
{
public static void Main(string[] args)
{
int a = 30;
int b = 40;
if (a < b)
{
Console.WriteLine("a's value is less than b");
}
else
{
Console.WriteLine("b's value is less than a");
}
}
}
}
登入後複製

輸出:

C# 三元運算符

在上面的程式中,定義了兩個變數a和b並分配了一些值。使用 if-else 條件語句將它們的值相互比較,找出較大的值。上面的程式由二十行程式碼組成,用於相互比較兩個值。使用三元運算子可以將相同的程式碼最佳化為十四行,如下程式碼中使用的:

2.使用三元運算子比較兩個值

考慮下面的 C# 程序,使用三元運算子比較兩個值。

代碼:

using System;
namespace Project
{
class MainClass
{
public static void Main(string[] args)
{
int a = 40;
int b = 30;
Console.WriteLine((a < b) ? "a's value is more than b" : "b's value is less than a");
}
}
}
登入後複製

輸出:

C# 三元運算符

在上面的程式中,定義了兩個變數a和b並分配了一些值。使用三元運算子將它們的值相互比較,找出較大的值。執行條件語句,並將語句的結果賦給變數 res。若條件語句的結果為真,則印出『?』之後的第二條語句,即a的值大於b;否則,印出『:』之後的第三條語句 b 的值小於 a。

3.嵌套三元運算子

當「?」或「:」後面的第二個參數或第三個參數又是條件語句時,此運算子稱為巢狀三元運算子。例如,考慮以下程序:

代碼:

using System;
namespace Project
{
public class MainClass
{
public static void Main(string[] args)
{
int a = 10;
int b = 8;
Console.WriteLine(a> b ? "a's value is more than b" : a < b ? "a's value is less than b" : a == b ? "C" : "No result");
}
}
}
登入後複製

輸出:

C# 三元運算符

在上面的程式中,定義了兩個變數a和b並分配了一些值。使用 C# 中的三元運算子將它們的值相互比較,以找出哪個較大或它們是否相等。執行條件語句,並將語句的結果賦給變數 res。如果條件語句的結果為true,則執行「?」之後的第二個語句,這也是一個條件語句a 注意:三元運算子不能執行語句。它只傳回第二部分或第三部分中存在的表達式或值,取決於第一部分中條件語句的結果。

C# 三元運算子範例

以下是 C# 中三元運算子的不同範例。

範例#1

使用巢狀三元運算子找出給定數字中最大的 C# 程式。

代碼:

using System;
using System.IO;
using System.Text;
//Define Namespace
namespace program
{
//Define class
public class large
{
//Define main method
public static void Main(string[] args)
{
//Define three variables to take the input
int x;
int y;
int z;
//Get the input from the users
Console.Write("First number must be entered : ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Second number must be entered: ");
y = Convert.ToInt32(Console.ReadLine());
Console.Write("Third number must be entered : ");
z = Convert.ToInt32(Console.ReadLine());
//largest number is found out by using nested ternary operator
int large = (x>y)?((x>z)?x:z):(y>z?y:z);
//display the largest number
Console.WriteLine("{0} is the largest number", large);
Console.ReadLine();
}
}
}
登入後複製

輸出:

C# 三元運算符

範例#2

使用三元運算子判斷給定數字是否為偶數的 C# 程式。

代碼:

using System;
// Define Namespace
namespace program
{
// Define class
public class check
{
// Define main method
public static void Main(string[] args)
{
//Assign the number which need to be checked if it is even or not
int number = 8;
bool ifitisEven;
//Logic to check if ithe given number is even or not
ifitisEven = (number % 2 == 0) ? true : false ;
Console.WriteLine(ifitisEven);
}
}
}
登入後複製

輸出:

C# 三元運算符

結論

在本教程中,我們透過定義來了解 C# 中三元運算子的概念,然後了解 C# 中三元運算子的工作原理。然後我們了解使用嵌套三元運算子和簡單三元運算子的不同 C# 程式以及它們的工作使用程式及其輸出快照,其中包含程式的結果。

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

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