C++之三目运算符
#include <iostream>
using namespace std;
int main() {
int x, y = 110;
x = (y < 10) ? 30 : 120;
cout << "conditional:" << x << endl;
return 0;
}
输出:
conditional:120
三目运算符根据?前面的表达式判断结果的值,若表达式为true,则输出?后面的值,若表达式为false,则输出:后面的值。