English

## Logic of a loop counter in a for-loop

 372 mar 2016.09.21 09:25   Hi coders,I have 10 objects in my chart. If I pop-up an alert with ObjectsTotal() the result is 10. When I want to color them all in green I do it this way:```for (int i=ObjectsTotal(); i>0; i--) {    ObjectSetInteger(0, ObjectName(0, i), OBJPROP_COLOR, clrGreen); } ```Now my question is this:  when I run this code, only 9 objects are colored green. If I change i>0 to i>=0 all 10 objects are colored. But why?ObjectsTotal() is 10. i>0 means that the loop should run as long as this condition is not met anymore. That is when i=0. So in my opinion i>=0 means that the loop runs 11 times instead of 10 times. The result shows me that I am wrong but can somebody explain me the logic? Why does the loop only run 9 times when using the code above?Thanks!  Edit:Now I changed the code to:```for (int i=ObjectsTotal(); i>0; i--) {    Alert(i+" "+ObjectName(0, i));    ObjectSetInteger(0, ObjectName(0, i), OBJPROP_COLOR, clrRed); }```The alert window looks like this:What's the problem with the first run of the loop?
 1235 FMIC 2016.09.21 09:42 #   Depending on what is done with the Objects, the correct way to "loop" is one of the following:```int ObjectCount = ObjectsTotal(); for( int i = ObjectCount - 1; i >= 0; i-- ) { ... } // or for( int i = 0; i < ObjectCount; i++ ) { ... }```Had you consulted the documentation, you would have seen this in the examples.
 2017 gooly 2016.09.21 10:27 #   I am using mainly the while loop:```int o = ObjectsTotal(); while (o-->0) {    ... ]```