if else 语句的编写方法

发布于 2024-12-12 03:15:30 字数 320 浏览 3 评论 0原文

可以使用三元运算符编写 if-else 语句,例如

output = (val>val2) ? "Condition is true" : "Condition is false";

现在考虑

if(condition1){
   //do something
}else if(condition2){
   //do something 
}else if(condition3){
  //do something
}

如何使用三元运算符编写上述代码?

An if-else statement can be written using ternary operator for e.g.

output = (val>val2) ? "Condition is true" : "Condition is false";

Now consider

if(condition1){
   //do something
}else if(condition2){
   //do something 
}else if(condition3){
  //do something
}

How to write above code using ternary operator ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

几度春秋 2024-12-19 03:15:30
output = (condition1) ? 
          "First Case" : ((condition2) ?
          "Second Case" : ( (condition3) ? 
          "Third Case") : (...));

但请记住,如果条件数量超过 3 个,维护将是一场噩梦。

output = (condition1) ? 
          "First Case" : ((condition2) ?
          "Second Case" : ( (condition3) ? 
          "Third Case") : (...));

But keep in mind that in case, the number of conditions become more than 3, it will be a maintenance nightmare.

黄昏下泛黄的笔记 2024-12-19 03:15:30

试试这个。

(a?w:(b?x:(c?y:z)))

Try this.

(a?w:(b?x:(c?y:z)))

—━☆沉默づ 2024-12-19 03:15:30

扩展形式如下:

X outputfn( ... )
{
  if(c1)      { return A; }
  else if(c2) { return B; }
  else        { return C; }
}

output = outputfn(...);

可以翻译成

output = (c1)?A:((c2)?B:C);

但它很难读。

所以我建议将其提取到一个函数中并使用该函数代替。

Something in expanded form like this:

X outputfn( ... )
{
  if(c1)      { return A; }
  else if(c2) { return B; }
  else        { return C; }
}

output = outputfn(...);

Can be translated into

output = (c1)?A:((c2)?B:C);

But its pretty unreadable.

So I'd recommened extracting it into a function and using the function instead.

风吹短裙飘 2024-12-19 03:15:30

我想我明白了。您将需要类似的内容:

output = (val>val3)?"First Case":(val>val2)?"Second Case":"Default Case";

您只需将后续的 if 放在表达式最右侧的子句中即可。请注意,最后总会有一个默认情况。

I think i get it. You'll want something like:

output = (val>val3)?"First Case":(val>val2)?"Second Case":"Default Case";

You simply put subsequent ifs in the right-most clause of the expression. Note that there will always be a default case at the end.

橪书 2024-12-19 03:15:30

当然。

您想要编写的是一个三元运算符作为 if else 语句。

好吧,让我们从逻辑上推断一下。

output = (val>val2) ? "Code here when condition is true" : "Code here when condition is false";

我们要说的是以下内容

if(val > val2)
{
    //code here when condition is true;
}
else
{
  //code here when condition is false;
}

就是这样!

快乐编码! ;)

Sure.

What you're trying to write is a ternary operator as an if else statement.

Well, let's logically deduce this.

output = (val>val2) ? "Code here when condition is true" : "Code here when condition is false";

What we're saying is the following

if(val > val2)
{
    //code here when condition is true;
}
else
{
  //code here when condition is false;
}

That's it!

Happy Coding! ;)

筱武穆 2024-12-19 03:15:30

我会这样格式化它:

int result = condition1 ? 1
  : condition2 ? 2
  : condition3 ? 3
  : -1;

但我同意其他海报的观点,即嵌套三元运算符可能很难理解。

I would format it this way:

int result = condition1 ? 1
  : condition2 ? 2
  : condition3 ? 3
  : -1;

But I concur with other posters that nested ternary operators can be difficult to understand.

黑凤梨 2024-12-19 03:15:30

为什么?您编写的内容非常清楚,使用三元运算符不会更改生成的代码。

Why? What you've written is perfectly clear, and using the ternary operator won't change the generated code.

迷离° 2024-12-19 03:15:30
output = (condition1) ? "Condition1 is true" : ((condition2) ?
      "Condition2 is true" : ( (condition3) ? 
      "Condition3 is true" : " all Condition are false"));
output = (condition1) ? "Condition1 is true" : ((condition2) ?
      "Condition2 is true" : ( (condition3) ? 
      "Condition3 is true" : " all Condition are false"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文