C++条件运算符Conditional operator(?)

它的格式为:

condition ? result1 : result2 (条件?返回值1:返回值2)

如果条件condition为真true,整个表达式将返回result1,否则将返回result。

8 == 6 ? 2 : 1   返回1,因为8不等于6.

5 > 3 ? 3 :4        返回3,因为5大于3.

//条件运算符例子

#include<iostream>   //这个头文件包括了 C++中定义的基本标准输入-输出程序库的声明。

using namespace std;

int main()

{

int a,b,c;

a = 2;

b =7;

c = (a>b)?a:b;

cout <<c;

return 0 ;

}结果输出7

猜你喜欢

转载自blog.csdn.net/m0_37350758/article/details/76068152
今日推荐