delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/10/22:37:11

From: loafman AT gte DOT net (Kenneth Loafman)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q] expression evaluation order
Date: 7 Aug 1997 17:31:25 GMT
Organization: Loafman Software
Lines: 26
Message-ID: <5sd0pd$k66$1@gte1.gte.net>
References: <5sbb1h$kvr$1 AT newton DOT pacific DOT net DOT sg>
Reply-To: loafman AT gte DOT net
NNTP-Posting-Host: dfw73123.gte.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Thu, 07 Aug 1997 17:15:11 GMT, xfesenko AT pacific DOT net DOT sg (Victor) wrote:

>Is it safe to assume that the expression 
>
>if(first_function()==OK && second_function()==OK &&
>third_function()==OK) return(OK);
>
>will be evaluated from left to right?
>
>Regards.
>Victor Fesenko

Yes, its called "Short Circuit Evaluation".  In an ANDing expression, the
expressions will be evaluated from left to right UNTIL one returns false.
I say this because, in your example, some functions may not be evaluated at
all and that could be a surprise if your are not expecting it.

The best example of this is "if (p && *p == 'a') {}" where the NULLness of
p would be checked in the first expression before deferencing it in the
second expression.

Although not guaranteed, most C's stop evaluation once an ORing expression
returns true from one of its expressions.  Another potential surprise.

...Kenneth

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019