Good practices with loops and counters

Hey All,

Is it a good practise to perform certain operations in your code based on the count value obtained from a complicated for loop? For example, lets say there are four objects you want to compare in your for loop, and based on the results of the comparison a certain count variable is incremented. And based on the value of count the code proceeds with performing different operations.

Is this a normal/good practise? Can you guys suggest something better?

Thanks! :)
Are you using the count value to check for condition to exit the loop? If so I suppose it depends on how certain you are that the comparasions you talk about are certain to drive the count to satisfy exit condition. I would built in some sort of guard in the event things don't go as you plan.
I suggest, you'd better use while loop.
That's a perfectly normal operation. It's called "counting".
Not sure if this is what you're talking about, but this is stupid:

1
2
3
4
5
6
7
8
9
for( int i = 0; i < 5; ++i ) {
    switch( i ) {
        case 0:  /* do something */ break;
        case 1: /* do something else */ break;
        case 2: /* do something different */ break;
        case 3: /* do something wild and crazy */ break;
        case 4: /* do something totally different */ break;
    }
}


(I've actually seen this in "professional" code.)
Topic archived. No new replies allowed.