Thalhammer
Thalhammer

Reputation: 375

for without increment vs while

I'm currently studying computer science in Germany but did work on several C/C++ opensource projects before. Today we kind of started with C at school and my teacher said it would be a no go to modify a for loop variable inside the loop, which I absolutely agree with. However, I often use a for loop without the last incrementing part and then modify it only inside the loop, which he also did not like.

So basically it comes down to

for(int i=0; i<100;) {
    [conditionally modify i]
}

vs

int i=0;
while(i<100) {
    [conditionally modify i]
}

I know that they are essentially the same after compile, but I don't like using a while loop because:

  1. It's common practice to limit variables to smallest possible scope
  2. It can introduce bugs if you reuse i (which you have to because of larger scope)
  3. You can not use a different type for i in a later loop without using a different name

Are there any style guides/common practices which one to choose ?

If you answer with "I like while/for more" at least provide a reason why you do so.

I did already search for similar questions, however, I could not find any real answer to this case.

Upvotes: 5

Views: 10945

Answers (4)

chqrlie
chqrlie

Reputation: 144770

If you are modifying the loop counter inside the body, I would recommend not using a for loop because it is more likely to cause reader's confusion than the while loop.

You can work around the lack of scope limitation with an additional block:

{
    int i = 0;
    while (i < 100) {
        [conditionally modify i]
    }
}

Upvotes: 1

H.S.
H.S.

Reputation: 12669

There are some common practices which programmers follow while taking the decision on which loop to use - for or while!

The for loop seems most appropriate when the number of iterations is known in advance. For example -

/*N - size of array*/
for (i = 0; i < N; i++) {
  ...
}

But, there could be many complex problems where the number of iterations depend upon a certain condition and can't be predicted beforehand, while loop is preferable in those situations. For example -

while( fgets( buf, MAXBUF, stdin ) != NULL)

However, both for and while loops are entry controlled loops and are interchangeable. Its completely up to the programmer to take the decision on which one to use.

Upvotes: 1

user2371524
user2371524

Reputation:

Style guides differ between different people / teams.

The C standard describes the syntax of for like this:

for ( clause-1 ; expression-2 ; expression-3 ) statement

and it's common practice to use for as soon as you have a valid use for "clause-1", and the reason to do so is indeed because of the limited scope:

[...] If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions; [...]

So, your argumentation is fine, and you could try to convince your teacher. But don't try too hard. After all, questions of style rarely have one definitive answer, and if your teacher insists on his rules, just follow them when coding for that class.

Upvotes: 3

marko
marko

Reputation: 77

    for(int i=0; i<100;) {
[conditionally modify i]
}

Because this looks confusing, not standard way to write for loop. Also, conditionally modify i is dangerous, you don't want to do that. Someone reading your code would have problems understanding how you increment, why, when..etc. You want to keep your code clean and easy to understand.

I would personally never write for loop with conditionally modifying iterator. If you need it, you can use additional counter, or something like that. But you shouldn't control flow with conditioning iterator, some people even avoid break and continue.

Upvotes: 0

Related Questions