c语言三元运算符_了解C / C ++中的三元运算符

c语言三元运算符

In this article, we’ll take a look at understanding the ternary operator in C / C++.

在本文中,我们将了解C / C ++中的三元运算符。

The ternary operator (? :) is a very useful operator, which is very similar to if-else statements. This is a useful option for programmers, to avoid cluttering the code with nested if-else conditions.

三元运算符( ? : :)是非常有用的运算符,它与if-else语句非常相似。 对于程序员来说,这是一个有用的选项,可以避免使用嵌套的if-else条件使代码混乱。

Let’s take a quick look at using this simple operator, using some illustrative examples!

让我们使用一些说明性示例快速浏览使用此简单运算符!

三元运算符 (The Ternary Operator)

Before coming to the ternary operator, let’s look at the format for a simple if-else statement.

在使用三元运算符之前,让我们看一下简单的if-else语句的格式。


if (condition) {
    variable = expression1;
}
else {
    variable = expression2;
}

While this is simple enough, it might be more convenient to write it in one line and assign to a variable directly!

尽管这很简单,但将其写成一行并直接分配给variable可能会更方便!

We can do this through the ternary operator.

我们可以通过三元运算符来实现。

The ternary operator works like this and is the same as the above if-else block.

三元运算符的工作方式与上面的if-else块相同。


variable = (condition != 0) ? expression1 : expression2;

See how simple it is?!

看看有多简单?!

What this says is that variable will be assigned the value of expression1 if condition holds true. Otherwise, it will be given the value of expression2.

这表示如果condition成立,则将为variable分配expression1的值。 否则,它将被赋予expression2的值。

So, this is essentially the same as the original ‘if-else’ statement.

因此,这基本上与原始的“ if-else”语句相同。

Let’s now look at a simple example.

现在让我们看一个简单的例子。

使用三元运算符-一些示例 (Using the Ternary Operator – Some Examples)

Consider the following program, which will assign variables to -1, based on whether it is odd or even.

考虑下面的程序,它将基于奇数还是偶数将变量分配给-1。


#include <stdio.h>

int main() {
    int a = 10;
    int b = 5;

    int c = (a % 2 == 0) ? a : -1;
    int d = (b % 2 == 0) ? b : -1;

    printf("a = %d, c = %d\n", a, c);
    printf("b = %d, d = %d\n", b, d);

    return 0;
}

Output

输出量


a = 10, c = 10
b = 5, d = -1

As you can see, since a is even, the first part (after ?) is now evaluated and assigned to c.

如您所见,由于a是偶数,因此现在将评估第一部分(在?之后)并将其分配给c。

While in the second case, since b is odd, the second part (after :) is now evaluated and assigned to d.

在第二种情况下,由于b为奇数,因此现在评估第二部分(在:之后)并将其分配给d。

Ternary operators also make writing recursive functions very easy to read.

三元运算符还使编写递归函数非常容易阅读。

Consider the below program, which uses a recursive function to find the fibonacci sequence, starting from 1, using ternary operators!

考虑下面的程序,该程序使用三元运算符从1开始使用递归函数查找斐波那契序列!

The recursive function will return 1 if n <= 1, and return fib(n-1) + fib(n-1) otherwise!

如果n <= 1 ,则递归函数将返回1,否则将返回fib(n-1) + fib(n-1)


#include <stdio.h>

int fibonacci(int n) {
    return (n <= 1) ? 1 : fibonacci(n-1) + fibonacci(n - 2);
}

int main() {
    int val = fibonacci(10);
    printf("Starting from 1, fibonacci(%d) = %d\n", 10, val);
    return 0;
}

Output

输出量


Starting from 1, fibonacci(10) = 89

If you work it out, you can indeed verify that the 10th fibonacci number after 1 is 89.

如果计算出来,您确实可以验证1之后的第十个斐波那契数是89。

The recursive function is now a simple one-liner due to the ternary operator.

由于三元运算符,递归函数现在是一个简单的线性函数。

Hopefully, this gives you another reason to use ternary operators more often!

希望这为您提供了另一个更频繁使用三元运算符的理由!

结论 (Conclusion)

We learned about using the Ternary operator (? :), which is a nifty trick using which we can avoid writing nested if-else conditions.

我们学习了如何使用三元运算符( ? : :),这是一个不错的技巧,可以避免编写嵌套的if-else条件。

This makes writing simple conditional statements in a single line, which is a good option to have for C / C++ programmers.

这使得可以在一行中编写简单的条件语句,这对于C / C ++程序员是一个不错的选择。

参考资料 (References)

翻译自: https://www.journaldev.com/40807/ternary-operator-in-c-plus-plus

c语言三元运算符

    原文作者:cunchi4221
    原文地址: https://blog.csdn.net/cunchi4221/article/details/107471058
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞