C语言 c - if + else if + else 在一行中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46540099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:43:10  来源:igfitidea点击:

c - if + else if + else in one line?

cif-statement

提问by Cjdcoy

I just got a question idea after having misunderstood a friend's statement.

在误解了朋友的陈述后,我刚刚有了一个问题的想法。

My friend told me: I just taught a colleague how to do a if/else in one line in c.

我的朋友告诉我:我刚刚教了一位同事如何在 c 中的一行中执行 if/else。

Example:

例子:

int i = 0;
i < 0 ? printf("i is below 0") : printf("i is over or equal to 0");

For now, nothing new, it's called a ternary and most people know about that kind of statement BUT I first understood that:

现在,没有什么新鲜事,它被称为三元,大多数人都知道这种说法,但我首先明白:

I just taught a colleague how to do a IF / ELSE IF / ELSE in one line. Since I don't / didn't know that doing such a thing is possible I tried to do something like

我刚刚教过一位同事如何在一行中做一个 IF / ELSE IF / ELSE。因为我不/不知道做这样的事情是可能的,所以我尝试做类似的事情

int i = 0;
 i < 0 ? printf("i is below 0") : i == 0 ? printf("i equal 0") : printf("i is over 0");

Is it actually possible to do a if / else if / else "ternary". Or is there a way to do such a thing without having an horrible piece of code?

实际上是否可以执行 if / else if / else “三元”。或者有没有办法在没有可怕的代码的情况下做这样的事情?

回答by Some programmer dude

If you see e.g. this conditional expression referenceyou can see that the format of a "ternary expression" is

如果你看到例如这个条件表达式引用,你可以看到“三元表达式”的格式是

condition ? expression-true : expression-false

condition ? expression-true : expression-false

All three parts of the conditional expressions are, in turn, expressions. That means you can have almost anykind of expression, including nested conditional (ternary) expressions in them.

条件表达式的所有三个部分都是表达式。这意味着您几乎可以拥有任何类型的表达式,包括其中的嵌套条件(三元)表达式。

回答by Superman

This is definitely valid.

这绝对是有效的。

Or you could try something like this -

或者你可以尝试这样的事情 -

printf(i < 0 ? "i is below 0" : i == 0 ? "i equal 0" : "i is over 0");

回答by Basile Starynkevitch

C has both statements and expressions. There are two different kinds of syntactical things. BTW lines don't matter much in C (except for the preprocessor).

C 有语句和表达式。有两种不同的句法事物。BTW 行在 C 中并不重要(预处理器除外)。

Expressions (like f(1,x+y)or even x=y++) are a special kind of statements (the most common one).

表达式(likef(1,x+y)或 even x=y++)是一种特殊的语句(最常见的语句)。

As an extension to C, the GCCcompiler adds statement expressions, beyond what the C11standard (read n1570) defines. Please download then read that n1570repoort.

作为 C 的扩展,GCC编译器添加了语句表达式,超出了C11标准(阅读n1570)定义的内容。请下载然后阅读n1570报告。

ifis for conditional statementsbut the ternary ?:operatoris for expressions (with all three operands being sub-expressions).

if用于条件语句,三元?:运算符用于表达式(所有三个操作数都是子表达式)。

Some programming languages (notably Lisp, Haskell, Scheme, Ocaml) have only expressions and don't have any statements.

一些编程语言(特别是 Lisp、Haskell、Scheme、Ocaml)只有表达式而没有任何语句。