#include<iostream>
using namespace std;
int main(void)
{
int a = -1;
a > 0 ? a = a : a = -a;
cout << a;
return 0;
}
输出结果为1
问号语句的意思是问号前面的语句是否为真,为真则执行冒号前面的语句,否则执行冒号后面的语句。
冒号前后必须有语句,不要以为不执行就可以放空,比如说把 a=a 删掉,放空,这是不行的,想要空语句可以写一个1之类的,如
#include<iostream>
using namespace std;
int main(void)
{
int a = -1;
a > 0 ? 1 : a = -a;
cout << a;
return 0;
}
问号语句的值
问号前面的值为真,则问号语句的值等于冒号前面的语句的值,否则为冒号后面的语句的值
#include<iostream>
using namespace std;
int main(void)
{
int a = -1;
int b = a > 0 ? 2 : a = -a;
cout << a << endl << b;
return 0;
}
输出
1
1
a=-a这句语句的值就是被赋值后的a的值。
#include<iostream>
using namespace std;
int main(void)
{
int a = 1;
int b = a > 0 ? 2 : a = -a;
cout << a << endl << b;
return 0;
}
输出
1
2
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。