首页  >  文章  >  后端开发  >  C# 中的条件运算符

C# 中的条件运算符

王林
王林原创
2024-09-03 15:08:32837浏览

C# 中的条件运算符顾名思义是指在 C# 程序中使用三个操作数。用于表示条件的操作数,条件为真时的值和条件为假时的值。 C# 中用于表示条件运算符的符号是‘? : ' (引号将被忽略,因为它们不是符号的一部分,用于区分符号)。

语法:

condition_expression ? first_expression : second_expression;

如何定义条件运算符?

第一个操作数在?之前指定并包含评估条件表达式的定义。定义表达式时需要小心,以便求值条件应始终产生布尔结果。指定布尔表达式或指定错误表达式不会导致编译错误。第二个表达式在: 符号之前指定,并保存定义或赋值,以防第一个表达式中定义的条件计算结果为 true。因此,如果第一个表达式中确定的返回值为 true,则计算第二个操作数。

定义中的第三个操作数包含表达式的定义,以防第一个操作数的条件结果计算为 false。或者,条件运算符也称为三元运算符或内联 if 运算符。 C# 中条件运算符的主要用途是作为 if-else 循环的替代方案,用于减少代码块的大小。条件运算符的另一个主要优点是,它将编译流程转换为分支语句,从而减少了所需的嵌套 if 语句的使用。

条件运算符遵循右关联原则,即运算从右到左分组。此外,由于条件运算符仅计算单个操作数值表达式,因此编译时间大大减少。第二个和第三个操作数的值仅限于赋值、递增和递减函数。

C# 中条件运算符的示例

让我们尝试了解使用条件运算符进行传统 C# 编程的方法。

示例#1

让我们首先尝试一个常规的 if else 语句:-

代码:

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 = 10, y = 25;
string result1;
if (x > y)
{
result1 = "value of x is greater than y";
}
else
{
result1 = "value of x is less than y";
}
Console.WriteLine(result1);
Console.ReadLine();
}
}
}

输出:

C# 中的条件运算符

上面的程序演示了一个简单的 if-else 语句,该语句比较两个变量 x 和 y 的值,并根据分配给它们的值打印结果,并且在评估条件时,x> > y.

示例#2

让我们尝试使用条件运算符 → 复制上面的程序。

代码:

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 = 10, y = 25;
string result1;
//using the Ternary Operator (?:) for the comparison of x and y variable
result1 = (x > y) ? "value of x is greater than y" : "value of x is less than y";
Console.WriteLine(result1);
Console.ReadLine();
}
}
}

输出:

C# 中的条件运算符

现在我们将确定条件运算符的主要用途,它通过提供自己的分支或嵌套条件来简化复杂的 if-else if 嵌套,与 if-else 相比,它的复杂性要低得多if 循环。

示例#3

为了理解这一点,让我们首先考虑一个常规的 if-else if 程序:-

代码:

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)
{
string comparisonResult;
int a = 25, b = 50;
if (a < b)
{
comparisonResult = "value of a is less than b ";
}
else if (a > b)
{
comparisonResult = " value of a is more than b";
}
else
{
comparisonResult = "a and b are both equal in value ";
}
Console.WriteLine(comparisonResult);
Console.ReadLine();
}
}
}

输出:

C# 中的条件运算符

示例#4

现在我们将了解如何使用条件运算符在 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)
{
string comparisonResult;
int a = 25, b = 50;
//Nested Ternary Operator (?:)
comparisonResult = (a < b) ? "value of a is less than b" : (a > b) ? "value of a is more than b" : "a and b are both equal in value";
Console.WriteLine(comparisonResult);
Console.ReadLine();
}
}
}

输出:

C# 中的条件运算符

因此,我们看到两者之间的差异,因为使用嵌套条件运算符大大降低了 if-else if 语句的复杂性。也了解了程序的流程,我们发现光标流程是正确的关联原则

(a < b) ? "value of a is less than b" : (a > b) ? "value of a is more than b" : "a and b are both equal in value";

对于像 A? 这样的语句,代码块使用单独的颜色表示。乙:丙? D : E 的计算结果与 (A ? B : C) 相同? D : E.

结论

条件运算符是用于求值和赋值运算的三个操作数运算符,对条件操作数的求值值(即 true 或 false)进行单独的赋值。条件运算符遵循右关联原则,即运算从右向左分组。条件运算符不能使用任何用户定义类型进行重载,并且仅限于 bool 值进行计算。条件运算符为冗长的 if-else if 语句提供了最合适的替代,并且还通过提供分支语句评估的机会来减少编译时间。

以上是C# 中的条件运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn