Where did the logic go?

### New Book Reviews!

 Where did the logic go?
Article Index
Where did the logic go?
Solution

## Solution

Most programmers assume that there is something odd going on with data typing but the answer is operator precedence.

The cause of the problem is the pesky assignment statement = which has a higher priority than the logical operator and.

What this means is that:

`\$inrange=\$a and \$b;`

is evaluated as

`(\$inrange=\$a) and \$b;`

because the assignment is higher priority and so is performed first.

That is, \$inrange is assigned the logical value stored in \$a, which in our example is true, and then the and is evaluated as the expression:

`true and \$b `

but the result of the operation is thrown away as the assignment operator has already been evaluated.

What this means is that you might have been expecting the \$inrange to be false, i.e the result of \$a and \$b but it turns out to be true because it's equal to \$a.

The solution to the problem is simple - brackets.

`\$inrange=\$a and \$b;`

you have to write:

`\$inrange=(\$a and \$b);`

with this change you get the expected result of 'out range' just as in the original form of the if statement.

## Pattern

This problem is very difficult to spot unless you have a table of operator precedences stored in your head.

The only foolproof pattern that avoids precedence problems without having to check the precedence of every operator is to overuse brackets.

It is often said that brackets are free so go ahead and overuse them, but clarity can be lost if you really write things like:

`\$answer=((3*2)+4);`

which gives the same answer if you leave out all the brackets.

In the case of logical operators the problem described is created because there are two versions of and and or.

You can write and or && and the only difference is precidence with the first having a lower precidence than asignment and the second a higher precidence. What this means is that:

`\$inrange=\$a && \$b;`

gives the correct or rather the expected answer of

`\$inrange=(\$a && \$b);`

In the same way there are two versions of or - or or || and the first has a precedence lower than assignment and the second a higher precedence.

So it look as if the best advice is to always use && and || and forget that and and or exist.

Unfortunately this isn't a complete solution as there is no alternative form of xor with a higher precedence and hence the xor operator always has a lower precedence than assignment so the problem occurs again if you write

`\$inrange=\$a xor \$b;`

expecting it to mean

`\$inrange=(\$a xor \$b);`

when it actually means

`(\$inrange=\$a) xor \$b;`

You have to use brackets.

## More Puzzles

 Sharpen Your Coding SkillsJigsaw Puzzles and The MacMahon SquaresAnother puzzle featuring Joe Celko's characterful pair, Melvin Frammis, an experienced developer at International Storm Door & Software, and his junior programmer sidekick, Bugsy Cottman. This cla [ ... ] + Full Story Sharpen Your Coding SkillsTaxicab Geometry ProblemsIn the conference season, developers face the perennial problem of getting from one hotel to another to meet colleagues. How good is your ability to write procedures to find shortest distance in a cit [ ... ] + Full Story Sharpen Your Coding SkillsSharpen your Coding Skills - Elevator PuzzleIntroducing Melvin and Bugsy, characters who figure in a series of challlenges from Joe Celko. Sharpen your coding skills with puzzles that will both amuse and torment you. + Full Story Other Articles