473,588 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

order in if(expression)

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


Mar 3 '06 #1
19 1827

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

Mar 3 '06 #2
> 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.

Mar 3 '06 #3

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.

Mar 3 '06 #4
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...
Mar 3 '06 #5
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
Mar 3 '06 #6
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
Mar 3 '06 #7

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

Mar 3 '06 #8
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
}

Mar 3 '06 #9

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().

Mar 3 '06 #10

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

Similar topics

6
1924
by: Chris | last post by:
Hi Folks, I would like to forward an expression to an if-statement via a variable. Like: $a=10; $b=20; $expr="$a > $b";#this should be my if-expression if ($expr) do something;#test should be if($a > $b)
39
2186
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A ? style A: .... .... int a = 1; if(0==a) {
4
1889
by: Jim Garrison | last post by:
OxygenXML is rejecting the following usage with "Unknown system function: if" <xsl:variable name="qd" select="if(value/qDisplay/text() eq '') then 'empty' else 'right'"/> Is this syntactically correct? If not, how should it read?
0
7862
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8228
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6634
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3847
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.