在"for"循环条件下使用"三元运算"是一种好习惯吗?

Abh*_*eet 2 c++ for-loop ternary-operator conditional-statements

在for循环条件中使用三元运算是一种好习惯.我问这个是因为我遇到过这样的情况:三元操作将解决我在循环条件中的问题.

例如:

for( short i = 0 ; i < count ; i++ ){
   for( short j = 0 ; j < ( ( x[i] < y[i] ) ? x[i] : y[i] ) ; j++ ){ //Here I am using ternary operation at for loop condition place
       //.....
       //.....Some Code Here .......
       //.....
   }
}
Run Code Online (Sandbox Code Playgroud)

Xeo*_*Xeo 6

我会去min(x[i], y[i]),因为它更清楚,但我没有看到您的代码立即出现问题.

  • 如果你确实想要更清楚,那么给出`j <x [i] && j <y [i]`的表达式是值得考虑的; 可能有类似的方法来考虑你的实际表达.... (4认同)