关于条件运算符 '?' ,有人可以用简单的 if else 格式向我解释一下吗?
Regarding conditional operator '?' , can some one explain this to me, in simple if else format?
关于条件运算符“?” ,有人可以用简单的 if else 格式向我解释一下吗?
AutoFFSuccess、ActSuccess、FUPSuccess 是布尔值。
char StatusCode = (AutoFFSuccess) ?
(ActSuccess) ?
'P' : 'W' : (FUPSuccess) ?
(ActSuccess) ?
'F' : 'G' : 'E';
我通常会这样格式化:
char statusCode = AutoFFSuccess ? ActSuccess ? 'P' : 'W'
: FUPSuccess ? ActSuccess ? 'F' : 'G'
: 'E'
或:
char statusCode;
if (AutoFFSuccess)
{
if (ActSuccess)
{
statusCode = 'P';
}
else
{
statusCode = 'W';
}
}
else
{
if (FUPSuccess)
{
if (ActSuccess)
{
statusCode = 'F';
}
else
{
statusCode = 'G';
}
}
else
{
statusCode = 'E';
}
}
虽然真的不太好看
我不会用嵌套的条件来表示它,而是使用 "stacked" 单个条件运算符将每个选项分开。请注意,有些人不喜欢这个,但我发现它很简单,一旦你习惯了:
char statusCode =
AutoFFSuccess && ActSuccess ? 'P'
: AutoFFSuccess ? 'W'
: FUPSuccess && ActSuccess ? 'F'
: FUPSuccess ? 'G'
: 'E';
或者更明确地说:
char statusCode =
AutoFFSuccess && ActSuccess ? 'P'
: AutoFFSuccess && !ActSuccess? 'W'
: !AutoFFSuccess && FUPSuccess && ActSuccess ? 'F'
: !AutoFFSuccess && FUPSuccess && !ActSuccess ? 'G'
: 'E';
(我假设条件不会在评估之间改变,请注意...)
像这样:
char StatusCode;
if (AutoFFSuccess)
{
if (ActSuccess)
StatusCode = 'P';
else
StatusCode = 'W';
}
else if (FUPSuccess)
{
if (ActSuccess)
StatusCode = 'F';
else
StatusCode = 'G';
}
else
StatusCode = 'E';
在伪代码中:
if AutoFFSuccess is true:
if ActSuccess is true:
StatusCode is 'P'
else:
StatusCode is 'W'
else:
if FUPSuccess is true:
if ActSuccess is true:
StatusCode is 'F'
else:
StatusCode is 'G'
else:
StatusCode is 'E'
if(AutoFFSuccess){
if(ActSuccess)
statuscode='P';
else
statuscode='W';
}
else if(FUPSuccess){
if(ActSuccess)
statuscode='F';
else
statuscode='G';
}
else statuscode = 'E';
关于条件运算符“?” ,有人可以用简单的 if else 格式向我解释一下吗? AutoFFSuccess、ActSuccess、FUPSuccess 是布尔值。
char StatusCode = (AutoFFSuccess) ?
(ActSuccess) ?
'P' : 'W' : (FUPSuccess) ?
(ActSuccess) ?
'F' : 'G' : 'E';
我通常会这样格式化:
char statusCode = AutoFFSuccess ? ActSuccess ? 'P' : 'W'
: FUPSuccess ? ActSuccess ? 'F' : 'G'
: 'E'
或:
char statusCode;
if (AutoFFSuccess)
{
if (ActSuccess)
{
statusCode = 'P';
}
else
{
statusCode = 'W';
}
}
else
{
if (FUPSuccess)
{
if (ActSuccess)
{
statusCode = 'F';
}
else
{
statusCode = 'G';
}
}
else
{
statusCode = 'E';
}
}
虽然真的不太好看
我不会用嵌套的条件来表示它,而是使用 "stacked" 单个条件运算符将每个选项分开。请注意,有些人不喜欢这个,但我发现它很简单,一旦你习惯了:
char statusCode =
AutoFFSuccess && ActSuccess ? 'P'
: AutoFFSuccess ? 'W'
: FUPSuccess && ActSuccess ? 'F'
: FUPSuccess ? 'G'
: 'E';
或者更明确地说:
char statusCode =
AutoFFSuccess && ActSuccess ? 'P'
: AutoFFSuccess && !ActSuccess? 'W'
: !AutoFFSuccess && FUPSuccess && ActSuccess ? 'F'
: !AutoFFSuccess && FUPSuccess && !ActSuccess ? 'G'
: 'E';
(我假设条件不会在评估之间改变,请注意...)
像这样:
char StatusCode;
if (AutoFFSuccess)
{
if (ActSuccess)
StatusCode = 'P';
else
StatusCode = 'W';
}
else if (FUPSuccess)
{
if (ActSuccess)
StatusCode = 'F';
else
StatusCode = 'G';
}
else
StatusCode = 'E';
在伪代码中:
if AutoFFSuccess is true:
if ActSuccess is true:
StatusCode is 'P'
else:
StatusCode is 'W'
else:
if FUPSuccess is true:
if ActSuccess is true:
StatusCode is 'F'
else:
StatusCode is 'G'
else:
StatusCode is 'E'
if(AutoFFSuccess){
if(ActSuccess)
statuscode='P';
else
statuscode='W';
}
else if(FUPSuccess){
if(ActSuccess)
statuscode='F';
else
statuscode='G';
}
else statuscode = 'E';