Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > order in if(expression)

Reply
Thread Tools

order in if(expression)

 
 
Babis Haldas
Guest
Posts: n/a
 
      03-03-2006
i have

if(i==1 && func1() ) do something

if i!=1 , func1 doesnt run at all
this is in my compiler

is this because the compiler or because the stantard
can i rely that in any system func1 will never run if i!=1
(so first agument is checked first)

thanks








 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      03-03-2006

Babis Haldas wrote:
> i have
>
> if(i==1 && func1() ) do something
>
> if i!=1 , func1 doesnt run at all
> this is in my compiler
>
> is this because the compiler or because the stantard
> can i rely that in any system func1 will never run if i!=1
> (so first agument is checked first)


That behaviour is standard. You can rely on it. If the left hand side
of && eavluates to false, the right hand side is not evaluated at all.

The logical-or operator || behaves similarly. If the left hand side
evaluates to true, the right hand side is not evaluated.

Gavin Deane

 
Reply With Quote
 
 
 
 
Ron Lima
Guest
Posts: n/a
 
      03-03-2006
> if(i==1 && func1() ) do something

If you need func1 to run, dispite the value of 1, then this expression
will not work for you, since if i is different from 1, the expression
containing fun1 will not be evaluated.

It is considered poor style relying on the evaluation order of
expressions since it may cause confusion on the reader of your code. It
is a better idea to rewrite your code in a way to get the function
calls outside expressions in order to make it clearer.

 
Reply With Quote
 
Jaspreet
Guest
Posts: n/a
 
      03-03-2006

Babis Haldas wrote:
> i have
>
> if(i==1 && func1() ) do something
>
> if i!=1 , func1 doesnt run at all
> this is in my compiler
>
> is this because the compiler or because the stantard
> can i rely that in any system func1 will never run if i!=1
> (so first agument is checked first)
>
> thanks


&& and || are called shortcut operators. && returns a TRUE only if
the expression on left and right side of the && operator is TRUE. In
your case, if 'i !=1' then there is no point in doing the right hand
side of the expression.

So basically you can rely on the standard and always assume that in
case of && if the first expression is FALSE the right hand side would
not be calculated.

Similarly, in-case of || if the first expression is TRUE the second
expression (on the right side) would not be calculated since || returns
a TRUE even if one of the expressions are TRUE.

 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      03-03-2006
Ron Lima wrote:
>> if(i==1 && func1() ) do something <snip> can i rely that in any
>> system func1 will never run if i!=1 (so first agument is checked
>> first)

>
> If you need func1 to run, dispite the value of 1, then this
> expression will not work for you, since if i is different from 1, the
> expression containing fun1 will not be evaluated.


I've included the bit you snipped, where he says he *does not* want it
to run if i != 1.

> It is considered poor style relying on the evaluation order of
> expressions since it may cause confusion on the reader of your code.


I think the use of "short-circuit" logical operators is pretty idiomatic
for programmers of many languages.

> It is a better idea to rewrite your code in a way to get the function
> calls outside expressions in order to make it clearer.


Having function calls outside of expressions is not always possible and
not necessarily clearer.

consider:

int myFunc getVal() {
return 42;
}

int main() {
int myInt = getVal();
}

How do you suggest I remove that function call from the expression?

Actually, in the case of initialisation, that's easy:

int myInt(getVal());

But not for assignment.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-03-2006
Babis Haldas posted:

> i have
>
> if(i==1 && func1() ) do something
>
> if i!=1 , func1 doesnt run at all
> this is in my compiler
>
> is this because the compiler or because the stantard
> can i rely that in any system func1 will never run if i!=1
> (so first agument is checked first)
>
> thanks


They call it "short-circuit analysis". The compiler doesn't need to know
what the second one evalutes to, so it doesn't bother.

However, if you need "func1" to be called, then simply chang:

if( i==1 && func1() )
{
; //Code
}

to:

if ( func1() )
{
if ( i == 1 )
{
; //Code
}
}


-Tomás
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-03-2006
Tomás posted:

> if( i==1 && func1() )
> {
> ; //Code
> }



Also, I forgot to mention. It is undefined whether "i == 1" will be
evaluated first, or if "func1()" will be evaluated first.

-Tomás
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      03-03-2006

Tomás wrote:
> Tomás posted:
>
> > if( i==1 && func1() )
> > {
> > ; //Code
> > }

>
>
> Also, I forgot to mention. It is undefined whether "i == 1" will be
> evaluated first, or if "func1()" will be evaluated first.


That's not true. In the code above, it is guaranteed that i==1 will be
evaluated first and it is guaranteed that func1() will only be
evaluated in the case that i==1 evaluated to true.

&& introduces a sequence point after the evaluation of the first
expression.

Gavin Deane

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-03-2006
Tomás wrote:

> However, if you need "func1" to be called, then simply chang:
>
> if( i==1 && func1() )
> {
> ; //Code
> }
>
> to:
>
> if ( func1() )
> {
> if ( i == 1 )
> {
> ; //Code
> }
> }


Or simply:

if (func1() && i == 1)
{
//Code
}

 
Reply With Quote
 
Jaspreet
Guest
Posts: n/a
 
      03-03-2006

Tomás wrote:
> Tomás posted:
>
> > if( i==1 && func1() )
> > {
> > ; //Code
> > }

>
>
> Also, I forgot to mention. It is undefined whether "i == 1" will be
> evaluated first, or if "func1()" will be evaluated first.
>
> -Tomás


I guess not. It is defined that i==1 would be calculated first and
depending on its result would the compiler work on evaluating / calling
func1().

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If you get an order # does it mean the order is accepted? =?Utf-8?B?U3RldmUxMDc3?= Windows 64bit 3 05-12-2005 11:46 PM
Two nics, change access order in REGISTRY key?? =?Utf-8?B?TWFjY2E3Nw==?= Wireless Networking 1 01-01-2005 08:32 PM
Traversion order cf. output order in XSL Soren Kuula XML 2 02-01-2004 09:10 AM
In which order are files looked for when loaded/requierd - and what'sthe order of suffixes? Stephan Kämper Ruby 2 01-18-2004 02:07 PM
How to Display DropDownList with preserved order (custom order) cspoh ASP .Net Web Controls 0 07-31-2003 09:19 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57