又如:设有定义:int a=6,b=7, 则:
min = a<b?a:b; min="6
min = a<b?++a:++b; min="7" a="7" b="7
min = a<b?a++:b++; min="6" a="7" b="7嵌套使用条件运算符还可以嵌套使用,如求三个数中的最大值:
if (a>=b) t=a; else t=b;
if (t>=c) max=t; else max=c;
可改写成:
max =(t=a>=b?a:b)>= c ? t : c;
又如:
if (a>b) cout<<″greater than″;
else if (a==b) cout<<″equal to″;
else cout<<″less than″;
可改写成:
cout<<(a>b? "greater than":a==b?"equal to":"less than");