c++for-looprestrictions

Are there restrictions in For Loops when putting more than one condition?


Is putting more than one condition into a for loop OK?

for example:

bool b = true;
for (int i = 0; i < 100 && b; i++)
    b = changeB(i); //Imagine this does something with b

So I was said that it's not correct to put that kind of conditions in a for loop. Any specific reasons? Or is it well done?


Solution

  • Why is putting more than one condition into a for loop not supposed to be OK?

    It's OK.

    So I was said that it's not correct to put that kind of conditions in a for loop. Any specific reasons?

    I suspect that the person who said that may know the specific reasons for their opinion.

    If you were to imagine a sequence of conditional expressions that are each more complex than the previous one, there will certainly be some point at which the condition becomes too complex to follow. At which point exactly does an expression become too complex to be readable in a loop condition is very subjective.


    Interestingly, even MISRA guidelines - some of which are subjectively silly and restrictive - explicitly allows the use of "other" loop control variables.