问题描述
我很好奇c ++如何处理此嵌套的条件运算符.我半确定自己了解这是如何工作的,但我很好奇,有人可以通过图表解释循环如何执行嵌套的条件运算符.
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 < 2) ? x[i] : y;
!i? y:x [1];
!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;
}
推荐答案
如有疑问,请清楚说明...
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];
}
}
只需简单地阅读一下声明. ?
之前的所有内容都放入if
,然后只需打开{
,直到我们看到:
.
Simply go through the statement. Everything before a ?
goes in an if
, then just open a {
until we see a :
.
这篇关于C ++嵌套条件运算符循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!