Re: evaluation of if statement

new topic     » goto parent     » topic index » view thread      » older message » newer message

Derek Parnell wrote:

> Hi James,
>
> this statement of yours:
> >    if 'Z' >= upper(input[position]) >= 'A' then
>
> is equivalent to:
>
>     if (('Z' >= upper(input[position])) >= 'A') then
>
> which breaks down to:
>
>     temp = ('Z' >= upper(input[position])
>     if temp >= 'A' then
>
> meaning that after the first comparision, that 'temp' is going to either
> have the value 0 or 1. Thus the next comparison is always false because
> both 0 and 1 and less than 'A'.
>
> Try this instead:
>
>    if 'Z' <= upper(input[position]) and
>       upper(input[position]) >= 'A' then

You can also make thinks look similar to James' code like so:

  ** some code **
if 'Z' >= upper(input[position]) and upper(input[position]) >= 'A' then
    --                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
end if
  ** other code **

Notice that all that has been added to James' code is an 'and' and the
object for comparison again.

Since that's a lot of extra typing, we could assign 'upper(input[position])'
to a variable. This will prevent Euphoria from searching the 'input'
sequence, then running the 'upper()' function twice, which is faster code,
and it saves on the fingers a little:

integer ch
  ** some code **
ch = upper(input[position])

if 'Z' >= ch and ch >= 'A' then
    -- do something
end if
  ** other code **

--
Carl

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu