By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,973 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,973 IT Pros & Developers. It's quick & easy.

return in conditon - why not?

P: n/a
I am quite new to c++, about half of a year.
I juct wonder, why such code is not possible:

int chlen(char * ar) {
for(int i=0;ar[i]||return i;i++);
}

In my opinion, it would be much "cuter" than this:

int chlen(char * ar) {
int i;
for(i=0;ar[i];i++);
return i;
}

Is there a reason, why return cound't be used as part of condition
(with true result, but interruptiong the function/program, so it is
not important).

eXander
Feb 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Feb 1, 6:26 pm, exande...@gmail.com wrote:
I am quite new to c++, about half of a year.
I juct wonder, why such code is not possible:

int chlen(char * ar) {
for(int i=0;ar[i]||return i;i++);

}

In my opinion, it would be much "cuter" than this:

int chlen(char * ar) {
int i;
for(i=0;ar[i];i++);
return i;

}

Is there a reason, why return cound't be used as part of condition
(with true result, but interruptiong the function/program, so it is
not important).

eXander
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();

Feb 2 '08 #2

P: n/a
On Fri, 1 Feb 2008 17:40:18 -0800 (PST), Christopher
<cp***@austin.rr.comwrote in comp.lang.c++:
On Feb 1, 6:26 pm, exande...@gmail.com wrote:
I am quite new to c++, about half of a year.
I juct wonder, why such code is not possible:

int chlen(char * ar) {
for(int i=0;ar[i]||return i;i++);

}

In my opinion, it would be much "cuter" than this:

int chlen(char * ar) {
int i;
for(i=0;ar[i];i++);
return i;

}

Is there a reason, why return cound't be used as part of condition
(with true result, but interruptiong the function/program, so it is
not important).

eXander

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?
Presumably he is defining a function to work with a C string, although
he did not specifically state that.
are you taking it for granted that the null character will indeed
evaluate to false?
Why are you questioning whether the null character evaluates to false?
C and C++ have guaranteed that it does for about 35 years now.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 2 '08 #3

P: n/a
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. :)
Feb 2 '08 #4

P: n/a
ex*******@gmail.com wrote:
...
Is there a reason, why return cound't be used as part of condition
(with true result, but interruptiong the function/program, so it is
not important).
...
'return' in C and C++ makes a return statement, not "return expression". You
can't use it as a part of another expression. The reason... Well, it was that
way in C, so it is that way in C++. Although, it could probably be made into an
expression in C++ without breaking legacy code. Anyway, nobody probably thought
this was really necessary.

Note that in C++ there's another remotely similar thing - 'throw' - which
happens to be an expression. It is a 'void' expression, though, not a 'bool'
one. Yet you can easily use it as a subexpression in another expression though
with the help of ',' operator or '?:' operator.

// A completely artificial/meaningless example
void chlen(char* ar)
{
for(int i = 0; ar[i] || (throw 0, false); i++);
}
In theory, something like this could've probably been done to 'return' as well
(i.e. make it a 'void' expression, not an 'bool' one with an artificial result).
But, once again, there's really not much need for that. (Although it would be
interesting to know what was the rationale behind making 'throw' an expression
instead of control statement).

--
Best regards,
Andrey Tarasevich
Feb 2 '08 #5

P: n/a
On 2008-02-02 06:30, ex*******@gmail.com wrote:
>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++?
I certainly hope not, there is no such language as C/C++.
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.
Actually you get undefined behaviour and the fact that your program does
not crash or something else bad happens is just luck.

--
Erik Wikström
Feb 2 '08 #6

P: n/a
ex*******@gmail.com wrote:
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.
There is lots of different structures that would be "basically the same"
and lots of different structures that would be "compiled to the same
code". Some are more readable (by humans) than others. So, given any two
structures that are basically the same, but one is more readable than
the other, which should we choose?
Feb 2 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.