1. What is short circuiting?

Lately, there has been talk of implementing the abaility to short circuit=

programs in Euphoria.  What is short circuiting?

--Alan
  =

new topic     » topic index » view message » categorize

2. Re: What is short circuiting?

Alan Tu wrote:

> Lately, there has been talk of implementing the abaility to short circuit
> programs in Euphoria.  What is short circuiting?

It's more correctly called short-circuit boolean evaluation. It goes like
this:

if days_worked > 0 and weekly_pay  / days_worked > 100 then
   puts(1,"You make more than $100 a day")
end if

Euphoria evaluates both parts of this every time: that is:
 days_worked > 0 (yes or no)
 weekly_pay / days_worked > 100 (yes or no) -- here's the problem, if
days_worked = 0
you'll get a divide by zero error here when trying to do the divide.

Some languages allow you to stop after the first evaluation fails, and not
even try the
second. Borland languages have a way to turn this feature off and on.

To avoid the divide error with Euphoria you have to write something like:
if days_worked > 0 then -- it's safe to divide now, it's not zero.
  if weekly_pay / days_worked > 100 then
     puts(.....)
  end if
end if

Regards,

Irv

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu