FOR循环语句中的IF语句?

use*_*774 2 c++ for-loop if-statement

我想把if语句放在我的for循环检查参数中,但我不知道怎么做,我想要这样,所以我不必复制我的代码并使其变得庞大,冗余和混乱.

这就是我想要做的

for( int i = elevator1CurrentStatus.floorNumber; if(boundary == 9) i<boundary else i>boundary ;i+=i+countDirection){

//Code Here 

}
Run Code Online (Sandbox Code Playgroud)

我该如何实现?基本上我想要测试语句说明是否向上或向下计数以依赖变量并根据该变量选择哪个方向.计数方向较早实现,为+1或-1.for(int i = elevator1CurrentStatus.floorNumber;(if(boundary == 9)iboundary;); i + = i + countDirection)

 for( int i = elevator1CurrentStatus.floorNumber; (if(boundary == 9) i<boundary else i>boundary) ;i+=i+countDirection)
Run Code Online (Sandbox Code Playgroud)

nam*_*ess 7

使用三元运算符:

for(int i = elevator1CurrentStatus.floorNumber; 
    boundary == 9 ? i < boundary : i > boundary; 
    i += i + countDirection) {

//Code Here 

}
Run Code Online (Sandbox Code Playgroud)

但我会将条件移到单独的函数/方法以提高可读性.