delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/01/17/06:44:54

To: djgpp AT delorie DOT com
Date: Mon, 17 Jan 2000 09:34:52 0000
From: "Nimrod Alonzo Abing" <synflood AT eudoramail DOT com>
Message-ID: <MIKMDOKPMHKIBAAA@shared1-mail.whowhere.com>
Mime-Version: 1.0
X-Sent-Mail: off
X-Mailer: MailCity Service
Subject: Re: Question on 'if' statements...
X-Sender-Ip: 208.160.246.197
Organization: QUALCOMM Eudora Web-Mail (http://www.eudoramail.com:80)
Reply-To: djgpp AT delorie DOT com

On Sun, 16 Jan 2000 21:53:16   Adam Schrotenboer wrote:
>Does it have any '||' in it?? Otherwise, the C def req's shortcut
>evaluation, so yes, if I understand correctly, it will exit out as soon as
>the statement is known to be false. (A little vague, I know. I only know a
>little bit from some comp.sci courses I took a few years back)
>

--

This is known as "short circuit" boolean eval. And it can happen even if a bool statement has an || in it. Example:

if (
       ((a==x)||(a==y))
    && ((b==x)||(b==y))
    && ((c==x)||(c==y))
   )
{
  do_something_cool();
}

If the '||' in the first paren group evaluates false, the whole statement bombs out even if the '||' in the second and third paren evaluates true.

Generally for the case:

if (a && b && c [&& ...])
{
  do_something_cool();
}

where a, b, c, can be a simple or complex boolean expression, if the first expression evaluates falls, the entire statement is false.

In another case:

if (
       ((a==x) || (a==y))
    && ((b==x) || (b==y))
    || ((c==x) || (c==y))
   )
{
  do_something_cool();
}

would require a complete boolean eval, since, simplified, it is equivalent to:

if (a && b || c)
{
  do_something_cool();
}

and '&&' has greater precedence than '||' and will require the entire statement to be evaluated.

Hope this helps...

oOOOo Synflood oOOOo



Join 18 million Eudora users by signing up for a free Eudora Web-Mail account at http://www.eudoramail.com

- Raw text -


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