问题
I'm curious about how c++ handles this nested conditional operator. I'm half sure that I understand how this works, but I'm curious, could anyone explain through a diagram how the loop would execute the nested conditional operator.
For example would the loop execute through the first expression of each conditional operator for each instance?
Also is this nested conditional operator structured as:
(i < 2) ? x[i] : y;
!i ? y : x[1];
I guess I'm just very curious about the nature of this. Please don't answer unless you are capable of giving me a thorough adequate explanation of how the loop executes this conditional operator.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const char x[2] [20] = {" Cloud "," at your service\n"};
const char * y = "Strife";
for (int i = 0; i < 7; i++)
cout << (( i < 2)? !i ? x [i] : y : x[1]);
cout << endl << endl << x[0] << endl << x[1] << endl;
cin.get();
cin.get();
return 0;
}
回答1:
When in doubt, spell it out...
for (int i = 0; i < 7; i++)
{
if (i < 2) {
if (!i) { // technically, this is "if i == 1"
cout << x[i];
} else { // and this is "if i == 0"
cout <<y;
}
} else {
cout << x[1];
}
}
Simply go through the statement. Everything before a ?
goes in an if
, then just open a {
until we see a :
.
回答2:
It seems you are asking how an expression like x ? y ? 1 : 2 : 3
is parsed.
If you think about it there is really only one possibility. Namely, the :
furthest to the right must bind to the ?
furthest to the left. Thus the expression parses as:
x ? (y ? 1 : 2) : 3
So, if x
and y
are true
, then 1
is returned; if x
but not y
is true
, then 2
is returned; and if x
is false
, then 3
is returned.
Sorry to not answer directly in the context of your problem but I felt like it would be easier to follow this way.
回答3:
The operator is not structured as you write. Maybe it is clearer with parentheses:
cout << ((i < 2) ? (!i ? x [i] : y) : x[1]);
回答4:
Some good analysis already of what the conditional means. Just wanted to contribute a couple suggestions:
- consider writing or reordering such expressions such that the
?
and:
alternate, - consider breaking them on to multiple lines with indentation reflecting their processing.
Either or both of these should make it easier to keep track of what they do.
Consider:
i < 2 ? !i ? x[i] : y : x[1] # somewhat confusing...
Just indenting to reveal processing precedence:
i < 2 // if just put ? and : beneath related condition
? !i // then if
? x[i] // then
: y // else
: x[1] // else
Or to simplify while keeping one-liner concision, try:
i >= 2 ? x[1] : !i ? x[i] : y # equivalent but simpler to "grok" (see below)
Expression ordered to alternate ?
and :
work like a simple if
/ else if
/ else if
/ else
chain, so you can process and eliminate possibilities steadily as you work your way through.
if (i >= 2)
(value is) x[1]
else if (!i)
x[i]
else
y;
I sometimes write alternating conditionals across lines too:
std::cout << (i >= 2 ? x[1] : // "if condition1 then value1 else
!i ? x[i] : // if condition2 then value2 else
y); // value3"
...or sometimes (depending on the line lengths and visual complexity) going the whole hog and lining up the :
beneath ?
...
std::cout << (i >= 2
? x[1]
: !i
? x[i]
: y);
As with many stylistic aspects of C++ source code, picking a nice layout is a bit of an art - but experimentation is a good way to get a feel for it.
来源:https://stackoverflow.com/questions/20712745/c-nested-conditional-operator-loop