What boolean value are you expecting "return i"; to evaluate to?
Are you wanting every iteration to return, since the condition is
evaluated every iteration?
If so, how is it that you are iterating at all? It would instantly
return
You're "cute" version doesn't make much sense either.
what boolean value are you expecting ar[i] to evaluate to?
what happens when i increments beyond the bounds of the array?
why iterate at all if there is no action to be taken?
are you just taking it for granted that someone will give you a c
style char array that is null terminated?
are you taking it for granted that the null character will indeed
evaluate to false?
I wouldn't recommend using c style char arrays anyway. If you want to
hold some text, use a std::string
std::string mytext = "This is some text";
int length = mytext.size();
I don't want to disrespect you in any way, but are you even
programming in c/c++?
Firstly, like Daniel T. said, there will by no return happening every
iteration, because seconod part of || (or) condition is not evaluted
if the first part is true.
Secondly, like Jack Klein said, it's been tens of years since NULL is
false and not NULL is true.
Next, when I iterate out of bound of the array nothing happens,
strlen() does exactly the same thing.
If you don't believe me, try yourself... :
int chlen(char * ar) {
int i;
for(i=0;ar[i];i++);
return i;
}
int main(int argc, char** argv) {
char a[1] = {'a','b','c','d'};
cout << strlen(a);
cout << chlen(a);
return (0);
}
Both functions just go out of bounds.
int charlen( const char* ar ) {
int result = 0; // always initialize your variables!
while ( ar[result] != 0 ) // I like to be explicit
++result;
return result;
}
It is basicly the same code as code as mine, I even think it'll be
compiled to the same code.
If the return statement is supposed to evaluate to true as you state
here, then what would happen with this code?
bool func() {
return false;
}
int main() {
while ( func() ) { }
}
func() returns false, what is mysterios about it? it is diametrally
different thing, what function returns and what will be the result of
return, for example if we say, that return returns always true
bool func() {
bool result = return false;
}
Than function return false, but in the bool variable result will be
true... if we could use it.
what would this mean?
int main() {
bool b = return 0;
}
In variable b is true, but function returns 0, i don't really see your
point.
or this?
bool foobar() {
return return false;
}
Returns false, because right return will be evalueted before left. I
still don't see your point.
I would really here some thoughtful ideas. :)