Reputation: 62185
I've read somewhere that one should use a for
loop when you are iterating over some sort of array/sequence/list/whatever you call it and a while loop when your loop needs to stop at a certain condition.
So, what if I have something like this
int len = 0;
for(; s[len] != '\n'; ++len) {
// some processing
if (someCondition) {
return len;
}
}
// if done iterating, return len
return len;
Is this okay to use the for
loop in this context or would a while
loop be preferable?
Upvotes: 0
Views: 242
Reputation: 1656
What about foreach!
int len = 0;
for each (char& c : s) {
// some processing
if (someCondition || c=='\n') break;
len++;
}
// if done iterating, return len
return len;
Upvotes: 1
Reputation: 1695
I see majority of the post are using for
.
This post here will give you idea in case you want to pursue with while
.
#include <stdio.h>
main ()
{
int counter=0;
while(somecondition) {
if (s[counter]!='\n')
{
//some processing
}
++counter;
}
}
return len;
Upvotes: 1
Reputation: 1074495
Given your quoted code, I'd use a for
loop, but I would put the initializer inside the for
statement as an assignment:
int len; // <== Not here
for (len = 0; s[len] != '\n'; ++len) { // <== Here
// some processing
if (someCondition) {
return len;
}
}
This is exactly what the for
structure is for: An initialization, test, and increment.
Upvotes: 2
Reputation: 2800
It really doesn't matter. They are equivalent in terms of efficiency. You could do either of the following:
for(; s[len] != '\n'; len++) { ... }
while(s[len] != '\n') { ... len++; }
The for
loop is perhaps more easily readable in this case, and the while
is perhaps a bit more clever, but they're really functionally equivalent.
EDIT: True, the for
doesn't increment the counter until after the first loop. Fixed it.
Upvotes: 1
Reputation: 279285
I'd probably write it this way:
int len;
for (len = 0; s[len] != '\n'; ++len) {
...
}
return len;
Just because then I say in the for
statement what the range is I'm interested in (index 0 up to the first newline character).
I generally choose between for
and while
on the basis of what the code looks like, rather than on where I stand in the hairsplitting semantics argument what the difference is between "some kind of range" vs "the period from now until some condition is false". I don't think it's a problem to play around a bit with for
. The third part of the statement is "specially visible" in the way that an increment at the end of a while
loop isn't, and I think that's enough to justify using it with a minor contortion like your empty first part.
Upvotes: 5
Reputation: 8273
Personally, I'd go with this:
int len = 0;
for (; s[len] != '\n'; ++len) {
// some processing
if (someCondition) {
break;
}
}
// if done iterating, return len
return len;
But really, it's all down to readability, as there's no real semantic difference between for
and while
loops. Use whatever format you feel conveys the meaning of your code the best. In the above case I chose for
because of the len
loop variable, if that wasn't there I would have went with while
.
Upvotes: 1
Reputation: 39906
It is a matter of taste. Both are equivalent.
But with years I have happend to really prefer the while loop over for loop. I prefer when lines are short, and the for has a tendency to put 3 different steps in the same line.
int i=0;
while(s[i] != '\n')
{
// some processing
if (someCondition)
{
return i;
}
++i;
}
but as i said, it's a matter of taste.
Upvotes: 1
Reputation: 79941
You're iterating, and a for
loop is more compact. You can achieve the same thing in either case anyway, but for this I prefer to use for
because you have all the loop drivers (condition, counter increase) in the beginning.
int len = 0;
while (s[len] != '\n')
{
// do stuff
if (something) return len;
++len;
}
return len;
vs.
int len;
for (len = 0; s[len] != '\n'; ++len)
{
// do stuff
if (something) return len;
}
return len;
Upvotes: 1
Reputation: 388
I would use a While and put the len
incrementation inside the condition verification s[++len] != '\n'
:D
But I guess it's what flavour you prefer really :)
Upvotes: 1
Reputation: 40857
Well, a for loop is the only one that's going to accept that kind of syntax so that's what I'd say you should use. Besides that...it actually doesn't really matter in the slightest. Do what is easy to read.
Upvotes: 2
Reputation: 13461
The for
loop is OK. You have the increment ++len
at the place of loop control, which I find preferable. You don't forget it somewhere at the end of the loop body. And it even works well when you use continue
.
Upvotes: 2