AEG*_*GIS 0 c++ conditional loops nested operator-keyword
我很好奇c ++如何处理这个嵌套的条件运算符.我很确定我理解它是如何工作的,但我很好奇,任何人都可以通过图解释循环如何执行嵌套的条件运算符.
例如,循环是否会为每个实例执行每个条件运算符的第一个表达式?
这个嵌套的条件运算符也是这样构造的:
(i <2)?x [i]:y;
!一世 ?y:x [1];
我想我对这个性质非常好奇.除非你能够给我一个关于循环如何执行这个条件运算符的充分解释,否则请不要回答.
#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;
}
Run Code Online (Sandbox Code Playgroud)
看来你在问如何x ? y ? 1 : 2 : 3解析表达式.
如果你考虑一下,实际上只有一种可能性.即,:最右边必须绑定到?最左边.因此表达式解析为:
x ? (y ? 1 : 2) : 3
所以,如果x和y是true,然后1返回; 如果x但不是yIS true,然后2返回; 如果x是false,则3返回.
很抱歉不能直接回答您的问题,但我觉得按照这种方式更容易.