473,406 Members | 2,698 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,406 software developers and data experts.

Order of functions in an IF

LSW
I've tried googling for the answer to this one, but haven't found anything.

In the following code snippet

BOOLEAN SomeValue;
if(SomeValue && SomeFunc())
DoSomething;

If SomeValue is False, will SomeFunc() be evaluated? Or will the
evaluation stop when a False is encountered in a boolean AND expression?

If it makes any difference, I'm currently using Turbo C++ 3.0 to develop
for an embedded system.

Thanks,
Jun 8 '07 #1
6 1813
LSW wrote:
I've tried googling for the answer to this one, but haven't found anything.

In the following code snippet

BOOLEAN SomeValue;
if(SomeValue && SomeFunc())
DoSomething;

If SomeValue is False, will SomeFunc() be evaluated? Or will the
evaluation stop when a False is encountered in a boolean AND expression?
no, it won't be evaluated. The guaranteed order is left-to-right. For
the same reason A || B doesn't evaluates B if A is true.
If it makes any difference, I'm currently using Turbo C++ 3.0 to develop
for an embedded system.
I don't know if it makes any difference, it shouldn't. This is an old C
standard rule, so i think every compiler it's compliant on it.

Regards,

Zeppe
Jun 8 '07 #2
Zeppe wrote :
LSW wrote:
>I've tried googling for the answer to this one, but haven't found anything.

In the following code snippet

BOOLEAN SomeValue;
if(SomeValue && SomeFunc())
DoSomething;

If SomeValue is False, will SomeFunc() be evaluated? Or will the
evaluation stop when a False is encountered in a boolean AND expression?

no, it won't be evaluated. The guaranteed order is left-to-right. For the
same reason A || B doesn't evaluates B if A is true.
Note that that guarantee alone doesn't mean that SomeFunc() will not be
evaluated if SomeValue is false, but yes, the default logical or and
and expressions do short-circuit :)

However, if BOOLEAN or the return-value of SomeFunc() were some
user-defined type with an overloaded operator&&(), it will most
definitely *not* short-circuit, which is probably a good reason why you
should not want to overload those operators.

- Sylvester
Jun 8 '07 #3
Sylvester Hesp wrote:
Zeppe wrote :
>LSW wrote:
>>If SomeValue is False, will SomeFunc() be evaluated? Or will the
evaluation stop when a False is encountered in a boolean AND expression?

no, it won't be evaluated. The guaranteed order is left-to-right. For
the same reason A || B doesn't evaluates B if A is true.

Note that that guarantee alone doesn't mean that SomeFunc() will not be
evaluated if SomeValue is false, but yes, the default logical or and and
expressions do short-circuit :)
Sure. It was an additional information: if one argument is true, the
other is not evaluated AND the guaranteed order of evaluation is
left-to-right.
However, if BOOLEAN or the return-value of SomeFunc() were some
user-defined type with an overloaded operator&&(), it will most
definitely *not* short-circuit, which is probably a good reason why you
should not want to overload those operators.
Which is an interesting and not obvious collateral effect. I didn't ever
think about that, but given that if you overload the operator && you are
defining a function that will be called with the second argument of &&
as argument, and being a sequence point before the function call, there
is no way to redefine such an operator and retain the correct behaviour.

This is equivalent to say that A && B can not be viewed as
operator&&(A,B). This is a little bit shocking, frankly! :)

Regards,

Zeppe
Jun 8 '07 #4
Zeppe wrote:
Sylvester Hesp wrote:
>However, if BOOLEAN or the return-value of SomeFunc() were some
user-defined type with an overloaded operator&&(), it will most
definitely *not* short-circuit, which is probably a good reason why
you should not want to overload those operators.

Which is an interesting and not obvious collateral effect. I didn't
ever think about that, but given that if you overload the operator &&
you are defining a function that will be called with the second
argument of && as argument,
Actually, both the left-hand side and the right-hand side are the
operator's _arguments_. They are both evaluated before the function
is called. The order of evaluation is, of course, unspecified.
and being a sequence point before the
function call, there is no way to redefine such an operator and
retain the correct behaviour.
:-) Under "correct" you mean the "built-in" behaviour, of course.
The behaviour of the overloaded operator can only be "incorrect" if
the compiler screws up somehow.
This is equivalent to say that A && B can not be viewed as
operator&&(A,B). This is a little bit shocking, frankly! :)
They cannot be that only for non-overloaded variation of &&.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '07 #5
Victor Bazarov wrote:
Zeppe wrote:
>Sylvester Hesp wrote:
>>However, if BOOLEAN or the return-value of SomeFunc() were some
user-defined type with an overloaded operator&&(), it will most
definitely *not* short-circuit, which is probably a good reason why
you should not want to overload those operators.
Which is an interesting and not obvious collateral effect. I didn't
ever think about that, but given that if you overload the operator &&
you are defining a function that will be called with the second
argument of && as argument,

Actually, both the left-hand side and the right-hand side are the
operator's _arguments_. They are both evaluated before the function
is called. The order of evaluation is, of course, unspecified.
it depends. If you redefine operator&& as a member argument of the class
A, A would be the member on which you apply the operator (which, of
course, is equivalent as considering it as the *this argument of a
binary operator).
>This is equivalent to say that A && B can not be viewed as
operator&&(A,B). This is a little bit shocking, frankly! :)

They cannot be that only for non-overloaded variation of &&.
Fair enough. But I always had this kind of naive idea that the behaviour
of all the built-in operators could be replicated by user defined
functions. :)

Regards,

Zeppe
Jun 8 '07 #6
On Jun 8, 4:59 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
Victor Bazarov wrote:
Zeppe wrote:
>Sylvester Hesp wrote:
>>However, if BOOLEAN or the return-value of SomeFunc() were some
>>user-defined type with an overloaded operator&&(), it will most
>>definitely *not* short-circuit, which is probably a good reason why
>>you should not want to overload those operators.
>Which is an interesting and not obvious collateral effect. I didn't
>ever think about that, but given that if you overload the operator &&
>you are defining a function that will be called with the second
>argument of && as argument,
Actually, both the left-hand side and the right-hand side are the
operator's _arguments_. They are both evaluated before the function
is called. The order of evaluation is, of course, unspecified.
it depends. If you redefine operator&& as a member argument of the class
A, A would be the member on which you apply the operator (which, of
course, is equivalent as considering it as the *this argument of a
binary operator).
So? The order of evaluation is still not defined, and both must
be evaluated.
>This is equivalent to say that A && B can not be viewed as
>operator&&(A,B). This is a little bit shocking, frankly! :)
They cannot be that only for non-overloaded variation of &&.
Fair enough. But I always had this kind of naive idea that the behaviour
of all the built-in operators could be replicated by user defined
functions. :)
That's not true in a lot of cases. There's no way to make a
user defined operator require an lvalue, for example. If a, b
and c are build in types, for example, "(a+b) = c" is illegal;
if they are class types, however, it's not.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
14
by: Joerg Schuster | last post by:
Hello, according to http://mail.python.org/pipermail/tutor/2001-July/007246.html the order of function definitions does matter in python. Does anyone know a trick to avoid this? Is there a...
6
by: Ian Sparks | last post by:
I have a python file with a number of functions named with the form doX so : doTask1 doThing doOther The order these are executed in is important and I want them to be executed top-down. They...
12
by: Matias Silva | last post by:
I have this list that i'm ordering by units_unit_id and is there way I can get a natural order so that D4 and D5 comes before D12 Thanks, Matt +---------------+ | units_unit_id |...
2
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
25
by: tienlx | last post by:
Hi all, i'm sorry about my poor English ( it isn't my native language). I confused the order which the function in C be evaluted in for() function ? Ex: i have 3 functions : a() b() and c() and...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
13
by: Thomas Mlynarczyk | last post by:
Hi, I have this code: class Test { public $status = 'dead'; function __construct() { $this->status = 'alive'; } function __destruct() { echo '<br>__destruct()'; } }
7
by: Ravi | last post by:
Suppose we are given a statement: f1(23,1) * f2(3) + f(4) can you please tell what is the order of evaluation of these functions?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.