c++ - increment a for loop by 1 then by 2 -


I have to increase from 1 to loop, then up to 2, then by 1 in this cycle. I have a way to do this, but I do not like it. Does anyone have a 'good' implementation with this:

  int value = 2; Int change value = 1; For (int i = 0; i & lt; m; i + = value) {value + = (changeValue * -1); }  

language is c ++

accepted the question The answer is gone, but no one does mention it, so I thought I would add an extra response:

2-1-2-1-2-1-2-1 by selecting the step you received indexed Here are:

  0, 2, 3, 5, 6, 8, 9, ...  

You can easily see that every third Index is disappearing from 1 The easiest way to write it ("the easiest" = "most obvious to read for developers, later") would be:

 for  (int i = 0; i & lt; M; ++ i) {if (i% 3 == 1) continues; / / Control the rest of the loop}  

There is no additional step variable here, and even if you repeat for the discarded index, there is no difference to display the trend It does not matter if you are not seeing performance - Critical Code.


Comments