-2

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?

Manugs
  • 9
  • 3
  • 9
    This is perfectly valid. From where did you get the impression that it is considered a bad practice? – Fureeish Dec 22 '17 at 01:32
  • *Why is putting more than one condition into a for loop not supposed to be OK* -- So how do you propose to change the code you posted if you didn't check the condition in the `for` loop, and have the loop behave exactly the same way? Whatever that is, does it look "better" than the posted code? – PaulMcKenzie Dec 22 '17 at 01:37
  • 2
    Do you think the logical intent of the code would be obvious if someone saw the code for the first time? If yes then your code is probably fine and you don't need to worry :). – George Dec 22 '17 at 01:37
  • This is fine practice while it could be slightly more efficient. – Jake Freeman Dec 22 '17 at 01:37
  • @Fureeish A friend of mine told me that using conditions not related with the "i" (or whatever you put int the first clause) was a bad programming practice, that's why my doubt. Thanks for the help – Manugs Dec 22 '17 at 01:38
  • 1
    *A friend of mine* -- Did your friend also show you how to write the alternative? This goes back to my previous comment. `for (int i = 0; i < 100; i++) { b = changeB(i); if ( !b ) break; }`. Does that version look simpler than the version you posted? – PaulMcKenzie Dec 22 '17 at 01:42
  • 2
    Well I think your friend might not know what he's talking about, or they were being very specific to another situation. – Austin Brunkhorst Dec 22 '17 at 01:42
  • @PaulMcKenzie with a while loop, but i think it looks worse, because I'd have to declare the "i" variable out anyway – Manugs Dec 22 '17 at 01:43
  • 3
    If your friend told you this was bad programming practice, I would think that they would be the person you would ask this question to. Was this advice given on their deathbed? – Benjamin Lindley Dec 22 '17 at 01:45
  • @PaulMcKenzie no, I didn't know what a break was used for; with break you just finish the loop and keep going with wathever is written after it? – Manugs Dec 22 '17 at 01:48
  • I wrote a loop like this in an answer in the past day or so. So I think it's OK. – Barmar Dec 22 '17 at 01:51
  • 1
    I don't expect you to depend on him for everything. But certainly he can give reasons for whatever advice he gives you. This forum is not made for telling you what your friend was thinking. – Benjamin Lindley Dec 22 '17 at 01:55
  • @Manugs The `break` exits the loop. But even my attempt isn't really equivalent, since `i` was not incremented on the terminating iteration. That's why your friend should have given you the equivalent loop that adheres to his advice -- just saying "don't do this" should require a follow-up "then what do I do?" – PaulMcKenzie Dec 22 '17 at 02:03
  • @PaulMcKenzie Well, what I did was use a while loop, where I was shure I could just put more conditions and that kind of things. Thanks for your answer and explanation!! – Manugs Dec 22 '17 at 02:15

2 Answers2

1

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

It's ok but most important to have clear variable names. B is not a descriptive enough name for example.

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40