473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

return in conditon - why not?

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
6 1811
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
1
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event,...
12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.